chanduvini
Posts: 1
Joined: Tue Feb 14, 2017 4:39 am

Read USB MBR Data and modify

Postby chanduvini » Thu Feb 16, 2017 9:57 pm

Hi All,
I wanted to read MBR data from USB plugged in to Raspberry Pi and modify first few bytes of it.
I was able to perform the same, with CreateFile, LOCK disk , ReadFile, Modify and writefile on the other Windows10 machines.

However, I face problems doing the same on raspberry Pi.
I do CreateFile based on drive letter. (\\.\<DriveLetter> ). But CreateFile with drive letter on Raspberry Pi fails.

Any help here will be highly appreciated.

Thanks.
k-pi
Posts: 138
Joined: Sun Feb 12, 2017 1:46 pm
Location: Upper Hale, Surrey, UK.

Re: Read USB MBR Data and modify

Postby k-pi » Fri Feb 17, 2017 1:10 pm

Likely, if you use the disk id you will get further. :)
Heater
Posts: 7252
Joined: Tue Jul 17, 2012 3:02 pm

Re: Read USB MBR Data and modify

Postby Heater » Fri Feb 17, 2017 2:59 pm

You can read and write any bytes form anywhere on a disk using the "dd" command:
https://en.wikipedia.org/wiki/Dd_(Unix_software)
jahboater
Posts: 1483
Joined: Wed Feb 04, 2015 6:38 pm

Re: Read USB MBR Data and modify

Postby jahboater » Fri Feb 17, 2017 3:09 pm

chanduvini wrote:However, I face problems doing the same on raspberry Pi.
I do CreateFile based on drive letter. (\\.\<DriveLetter> ). But CreateFile with drive letter on Raspberry Pi fails.
CreateFile is a Windows API function, and "drive letters" are a Windows thing. The Raspberry Pi runs a different operating system called Linux.

As Heater says, dd is your friend.

Code: Select all

sudo dd if=/dev/sda  of=myfile  bs=512  count=1
will read the first 512 bytes (the boot sector) into myfile (change as appropriate).
Swap "if" and "of" to write it back after you have made your changes.

Of course you can write your own program with open(), read(), lseek(), write(), and close() to do the same thing, and your changes, all in one go.

Take care!
Aydan
Posts: 552
Joined: Fri Apr 13, 2012 11:48 am
Location: Germany, near Lake Constance

Re: Read USB MBR Data and modify

Postby Aydan » Fri Feb 17, 2017 3:14 pm

in linux you can use a blockdevice like a file.

In python for example:

Code: Select all

diskpath="/dev/sdx"
f=open(diskpath,"rb+") #open disk readwrite binary
#read MBR (2nd block)
f.seek(512)
MBR=f.read(512)
#modify MBR

#write MBR
f.seek(512)
f.write(MBR)
run as root

Regards
Aydan
jahboater
Posts: 1483
Joined: Wed Feb 04, 2015 6:38 pm

Re: Read USB MBR Data and modify

Postby jahboater » Fri Feb 17, 2017 3:18 pm

Thats cool! But why the seek(512)? surely it starts at offset zero. After changing the MBR a seek(0) would be needed in order to rewind the file and write out the new MBR starting at zero. Or have I missed something?
Aydan
Posts: 552
Joined: Fri Apr 13, 2012 11:48 am
Location: Germany, near Lake Constance

Re: Read USB MBR Data and modify

Postby Aydan » Fri Feb 17, 2017 3:30 pm

jahboater wrote:Thats cool! But why the seek(512)? surely it starts at offset zero. After changing the MBR a seek(0) would be needed in order to rewind the file and write out the new MBR starting at zero. Or have I missed something?
Oops, you are correct: https://en.wikipedia.org/wiki/Master_boot_record

@chanduvini:
If you only want to change a few values you could also use hexedit (available as debian/raspbian package)
It lets you edit files/partitions/disks in hex mode

Regards
Aydan