Replacing values in a binary file

Programming, for all ages and all languages.
Post Reply
User avatar
b.zaar
Member
Member
Posts: 294
Joined: Wed May 21, 2008 4:33 am
Location: Mars MTC +6:00
Contact:

Replacing values in a binary file

Post by b.zaar »

Hi, I'm looking for a way to replace a few bytes in a binary file from a script. Something similar to MS debug's e command through an input file.

The binary file is a boot sector but the values need to be changed for different disk types. I could write a small C program to do this easily but I'm wondering if it's possible to do from a script/makefile command line under linux.

This is only a temp solution until the full mkfs code is written to use the boot sector.

Thanks for any help.
"God! Not Unix" - Richard Stallman

Website: venom Dev
OS project: venom OS
Hexadecimal Editor: hexed
TX0
Posts: 1
Joined: Tue Jun 15, 2010 7:47 am

Re: Replacing values in a binary file

Post by TX0 »

Have a look at xxd command.
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Re: Replacing values in a binary file

Post by Brynet-Inc »

Hex editor.
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Replacing values in a binary file

Post by Solar »

Depending on what exactly you're planning to do, you could plant "marker" values in the binary, i.e. character sequences you know for sure won't appear in the file legally, and search & replace them using sed.

At least, that's what I would do.
Every good solution is obvious once you've found it.
js
Member
Member
Posts: 38
Joined: Thu Feb 26, 2009 1:45 am

Re: Replacing values in a binary file

Post by js »

Code: Select all

hexdump -Cv < myfile.bin > myfile.dump # makes a hex dump of the file
Then modify the dump in your script (sed, …). You only need to change the hex part (on the left), not the "ascii preview" (on the right).
Then :

Code: Select all

hexdump -R < myfile.dump > myfile.bin.modified # makes a file out of the hex dump
Otherwise, if you just need to change a specific portion of the file, use :

Code: Select all

dd if=patch.bin bs=1 seek=<offset in myfile.bin> of=myfile.bin conv=notrunc # optionnaly add : count=<length of patch>
EDIT:
IIRC, you can get the position in the file of a specific function or whatever by using objdump and grepping through it. That might be usefull for method 1 (hexdump).
User avatar
b.zaar
Member
Member
Posts: 294
Joined: Wed May 21, 2008 4:33 am
Location: Mars MTC +6:00
Contact:

Re: Replacing values in a binary file

Post by b.zaar »

I've started a project called hexed for a solution to this.

Info will be updated here http://forum.osdev.org/viewtopic.php?f= ... 42&start=0
"God! Not Unix" - Richard Stallman

Website: venom Dev
OS project: venom OS
Hexadecimal Editor: hexed
Post Reply