Page 1 of 1
Replacing values in a binary file
Posted: Tue Jun 08, 2010 8:31 pm
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.
Re: Replacing values in a binary file
Posted: Tue Jun 15, 2010 8:04 am
by TX0
Have a look at xxd command.
Re: Replacing values in a binary file
Posted: Tue Jun 15, 2010 3:26 pm
by Brynet-Inc
Hex editor.
Re: Replacing values in a binary file
Posted: Wed Jun 16, 2010 1:42 am
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.
Re: Replacing values in a binary file
Posted: Thu Jun 17, 2010 12:10 pm
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).
Re: Replacing values in a binary file
Posted: Sat Oct 16, 2010 2:34 am
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