You are not logged in.

#1 2008-12-21 22:11:57

LiteHacker
Member
Registered: 2008-11-27
Posts: 38
Website

Bash command to append/modify specific byte in a file?

Hello,

I've been programming with nasm for some time. Usually when I wanted to use more than one file, I would simply "%include" it......

Recently I tried using ld to link multiple assembly files together... After a while, I finally got it to mostly work how I wanted.

Now I am trying to write up a boot sector using multiple files. Now by the requirements of boot sectors, the sector must be 512 bytes and the last 2 bytes must be 0x55 and 0xAA (or 0xAA55).

Usually in assembly I would simply do something like this at the end of the code:

times 510-($-$$) db 0           ; Fill up the file with zeros

        dw 0xAA55                ; Boot sector identifier

However if I have this code + additional assembly code, the additional assembly code gets padded on, thus the size becomes more than 512 bytes.

I have concluded that ld cannot somehow attach the information into 512 bytes... or otherwise it would be difficult to get it to do so.
So I have modified my strategy. Instead of padding on a lot of spaces and the 0xAA55 in assembly code, it seems to be more efficient to first compile and link everything.. and only afterwards, pad on the extra bytes and add in the 0xAA55 into the file raw binary.

I have succeeded in making the file raw binary.. currently it's about 90 bytes (plenty of space before I hit 512)... Does anybody know how to pad on the extra bytes and add on byte 0x55 and byte 0xAA at the end to make the file final size 512?

A command line function to use would be much appreciated.

Thank you,
LiteHacker

Offline

#2 2008-12-21 22:46:16

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Bash command to append/modify specific byte in a file?

If you first make one file consisting of
0000000...AA55    (template)

then you can use dd:

dd if=bootsector of=template conv=notrunc

Offline

#3 2008-12-21 23:12:51

LiteHacker
Member
Registered: 2008-11-27
Posts: 38
Website

Re: Bash command to append/modify specific byte in a file?

Well, it worked. Thank you Procyon.
Interesting... I didn't know "dd" left all other data in "of" the same if it wasn't overwritten.

Well, we learn something new every day. smile

If anybody thinks they know a better way not requiring writing an extra file, feel free to post. Otherwise, this method works great.

LiteHacker

Offline

#4 2008-12-22 00:13:42

tam1138
Member
Registered: 2007-09-10
Posts: 238

Re: Bash command to append/modify specific byte in a file?

Alternatively, if you've got a file (eg, "trailer") containing AA 55 and no newline, you can just use cat with the append redirection:

cat trailer >> file

Offline

#5 2008-12-22 03:35:19

LiteHacker
Member
Registered: 2008-11-27
Posts: 38
Website

Re: Bash command to append/modify specific byte in a file?

Okay, new question:

I currently have an image of one sector. I would like to append another file to it. (This other file represents one or more other sectors)... Both of these files are binary files. From what I understand, "cat" seems to only work with regular printable characters. Please correct me if I am wrong with that.

So, if I am not wrong about cat, how would you concatenate these two binary files?

Thank you,
LiteHacker

Offline

#6 2008-12-22 04:09:28

LiteHacker
Member
Registered: 2008-11-27
Posts: 38
Website

Re: Bash command to append/modify specific byte in a file?

okay, I figured it out:

cat file1 file2 > concatenatedfile

Offline

Board footer

Powered by FluxBB