Google
 
Web ppczone.net

View Full Version : SD Card - boot sector


Peter Sommerfeld
01-11-2006, 01:05 PM
Hi guys,

I am finding that reading the raw data off an SD in my embedded, that
the boot sector is not actually at address 0, but differs from card to
card (ie. my 64 MB has the boot sector at 4E00, and my 128 MB at C600).

Everything else before this is read as 0's.

I am not using any of the secure stuff on the card, just plain-old SPI.
I verifed I am reading the exact values vs. WinHex on a PC and card
reader.

Can anyone provide insight into this? Is there some non-readable secure
stuff on SD cards before the boot sector? Any ideas where I get more
info on this? (I have tons of info on the FAT field descriptions, etc
already).

Thanks!

-- Pete

larwe
01-11-2006, 01:28 PM
> I am finding that reading the raw data off an SD in my embedded, that
> the boot sector is not actually at address 0, but differs from card to
> card (ie. my 64 MB has the boot sector at 4E00, and my 128 MB at C600).

What boot sector are you talking about - the DOS boot sector (first
sector of the FAT volume) or the MBR?

psommerfeld@gmail.com
01-11-2006, 02:20 PM
It's the one where the 1st byte is EB and offset 3 in the sector has
the string "MSDOS5.0" on my FAT32-formatted card. I apolozige for not
knowing the correct term, but it must be the DOS boot sector?

-- Pete

larwe
01-11-2006, 02:25 PM
> It's the one where the 1st byte is EB and offset 3 in the sector has
> the string "MSDOS5.0" on my FAT32-formatted card. I apolozige for not
> knowing the correct term, but it must be the DOS boot sector?

That's the LBR. Removable flash media are formatted as hard disks. They
have an MBR (partition table) at sector 0. If you formatted the card on
Windows the MBR is probably set up so that the first partition is at
cylinder 0 head 1 sector 1 (i.e. you lose the first track on the disk).

Look at my DOSFS code for information on the MBR format and C code to
work out the start of the partition. http://www.zws.com/products/dosfs/

Maxim S. Shatskih
01-11-2006, 05:21 PM
The card can have the MBR partition table. In this case, the boot sector is
usually sector 63.

--
Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
maxim@storagecraft.com
http://www.storagecraft.com

"Peter Sommerfeld" <psommerfeld@gmail.com> wrote in message
news:1137006351.757006.163310@g43g2000cwa.googlegr oups.com...
> Hi guys,
>
> I am finding that reading the raw data off an SD in my embedded, that
> the boot sector is not actually at address 0, but differs from card to
> card (ie. my 64 MB has the boot sector at 4E00, and my 128 MB at C600).
>
> Everything else before this is read as 0's.
>
> I am not using any of the secure stuff on the card, just plain-old SPI.
> I verifed I am reading the exact values vs. WinHex on a PC and card
> reader.
>
> Can anyone provide insight into this? Is there some non-readable secure
> stuff on SD cards before the boot sector? Any ideas where I get more
> info on this? (I have tons of info on the FAT field descriptions, etc
> already).
>
> Thanks!
>
> -- Pete
>

Maxim S. Shatskih
01-11-2006, 05:25 PM
> That's the LBR. Removable flash media are formatted as hard disks.

Usually. This depends upon its controller firmware, whether it reports itself
as removable media or not.

If not - then it will have MBR, and will have the "hard disk" icon in Windows.

If yes - it will have no MBR (boot sector at sector 0) and will have the
"diskette drive" icon in Windows.

Windows does not support MBR and partitions on removable media, but requires
the partition table (MBR, GPT or Dynamic Disk) on a non-removable media.

--
Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
maxim@storagecraft.com
http://www.storagecraft.com

Ross Marchant
01-11-2006, 05:51 PM
"Maxim S. Shatskih" <maxim@storagecraft.com> wrote in message
news:dq43vb$rba$1@gavrilo.mtu.ru...
>> That's the LBR. Removable flash media are formatted as hard disks.
>
> Usually. This depends upon its controller firmware, whether it reports
> itself
> as removable media or not.
>
> If not - then it will have MBR, and will have the "hard disk" icon in
> Windows.
>
> If yes - it will have no MBR (boot sector at sector 0) and will have the
> "diskette drive" icon in Windows.
>
> Windows does not support MBR and partitions on removable media, but
> requires
> the partition table (MBR, GPT or Dynamic Disk) on a non-removable media.
>
> --
> Maxim Shatskih, Windows DDK MVP
> StorageCraft Corporation
> maxim@storagecraft.com
> http://www.storagecraft.com
>

Sometimes removeable media come with the MBR / partition table and so
formatting will retain will retain the MBR. However if the disk is
subsequently damaged then windows will totally reformat the disk, with no
MBR and the dos boot sector as sector 0. It is worthwhile checking for both
instances. I do this by checking if the first byte of sector 0 is 0xEB.

Ross

larwe
01-11-2006, 07:18 PM
Maxim S. Shatskih wrote:

> Windows does not support MBR and partitions on removable media, but requires
> the partition table (MBR, GPT or Dynamic Disk) on a non-removable media.

In a practical sense: all embedded systems that I have seen (cameras,
mainly) that use removable media will format it as a hard disk. I
suspect that most media playback devices would not recognize a "big
floppy" flash disk. Some flash media specifications explicitly require
an MBR (SSFDC comes to mind).

All the USB adapters I've used - which is many different types - cause
Windows to format cards with an MBR. As a matter of interest, the units
I have also report as removable to Windows.

It's safe to say that anybody who doesn't format flash media with an
MBR is being a maverick.

larwe
01-11-2006, 07:20 PM
Maxim S. Shatskih wrote:
> The card can have the MBR partition table. In this case, the boot sector is
> usually sector 63.

That's an amazingly broad generalization, broad to the point of being
dangerously wrong. It depends entirely on the size of the card -
because it's determined by the reported CHS geometry of the card,
presumably for legacy FDISK reasons. The LBR is generally _one track_
away from the MBR.

For the specific sizes of SD card you've used in recent times, maybe
spt was always 63.

The OP's post clearly shows that your generalization isn't even true
for the cards he happens to have lying about.

larwe
01-11-2006, 07:23 PM
Ross Marchant wrote:

> Sometimes removeable media come with the MBR / partition table and so
> formatting will retain will retain the MBR. However if the disk is
> subsequently damaged then windows will totally reformat the disk, with no
> MBR and the dos boot sector as sector 0. It is worthwhile checking for both
> instances. I do this by checking if the first byte of sector 0 is 0xEB.

What kind of interface were you using to access the drive?

I just did the following: On a linux box:

dd if=/dev/zero of=/dev/sda bs=512 count=100

then using the inbuilt O2Micro SD reader slot in my laptop, reformatted
the card in Windows. The resulting card had an MBR with one partition
(not marked active).

So I don't think it's as consistent as this.

Beverly Howard [Ms-MVP/MobileDev]
01-11-2006, 08:24 PM
While I can't speak to the structure of the card itself, it's been my
observation that the card is only part of the equation... the reader
plays a bigger part with respect to "booting" devices and it's ability
to do so is dependant on both the computer's ability to boot from the
connection method and that computer's current bios configuration has to
be set to allow such a boot.

Of about half a dozen different usb card readers, I have two that show
up as usb boot devices on my system's boot selector menu and I have only
two computers out of about seven which offer the capability to boot from
usb devices... for example, my Sony Vaio Picturebook will boot via usb,
but it will only recognize one specific usb floppy drive as a valid boot
device.

With those two bootable SD readers, I can then use different boot
configurations stored on different SD cards to boot the pc.

Tip... disconnect all usb devices except a card reader and follow your
box's alternate boot steps... if the reader is bootable, it will show in
the boot device menu under an interesting variety of device names.

Beverly Howard [MS MVP-Mobile Devices]

Paul Black
01-12-2006, 03:31 AM
Maxim S. Shatskih wrote:
> Windows does not support MBR and partitions on removable media,

What about USB and Firewire disks?

Paul

Grant Edwards
01-12-2006, 09:11 AM
On 2006-01-12, Paul Black <nospam@nospam.oxsemi.com> wrote:
> Maxim S. Shatskih wrote:
>> Windows does not support MBR and partitions on removable media,
>
> What about USB and Firewire disks?

They're not "removable".

--
Grant Edwards grante Yow! Hmmm... a CRIPPLED
at ACCOUNTANT with a FALAFEL
visi.com sandwich is HIT by a
TROLLEY-CAR...

larwe
01-12-2006, 09:19 AM
Grant Edwards wrote:

> >> Windows does not support MBR and partitions on removable media,
> >
> > What about USB and Firewire disks?
>
> They're not "removable".

Depends on the disk. What about ZIP disks?

My cheapo USB-to-laptop-drive adapters mount as a removable drive (they
show the "socketed drive" icon and have an Eject option on the context
menu).

Grant Edwards
01-12-2006, 09:23 AM
On 2006-01-12, larwe <zwsdotcom@gmail.com> wrote:
>
> Grant Edwards wrote:
>
>> >> Windows does not support MBR and partitions on removable media,
>> >
>> > What about USB and Firewire disks?
>>
>> They're not "removable".
>
> Depends on the disk. What about ZIP disks?

Right. For some reason I thought we were talking about
USB and Firewire hard-drives. Other types of USB mass-storage
show up as removable (Zip disks, CF readers, etc.).

--
Grant Edwards grante Yow! MERYL STREEP is my
at obstetrician!
visi.com

Maxim S. Shatskih
01-12-2006, 10:37 AM
> Sometimes removeable media come with the MBR / partition table and so
> formatting will retain will retain the MBR. However if the disk is
> subsequently damaged then windows will totally reformat the disk, with no
> MBR and the dos boot sector as sector 0. It is worthwhile checking for both

Exactly. Saw this personally on Iomega ZipDrive.

--
Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
maxim@storagecraft.com
http://www.storagecraft.com

psommerfeld@gmail.com
01-12-2006, 10:40 AM
larwe wrote:
> > It's the one where the 1st byte is EB and offset 3 in the sector has
> > the string "MSDOS5.0" on my FAT32-formatted card. I apolozige for not
> > knowing the correct term, but it must be the DOS boot sector?
>
> That's the LBR. Removable flash media are formatted as hard disks. They
> have an MBR (partition table) at sector 0. If you formatted the card on
> Windows the MBR is probably set up so that the first partition is at
> cylinder 0 head 1 sector 1 (i.e. you lose the first track on the disk).
>
> Look at my DOSFS code for information on the MBR format and C code to
> work out the start of the partition. http://www.zws.com/products/dosfs/

Ok, so does it make sense that the MBR, (ie. everything leading up to
the LBR), is all 0's on my SD cards? The were formatted as FAT32 under
Win XP. Shouldn't the MBR contain some non-0 data?

-- Pete

Maxim S. Shatskih
01-12-2006, 10:41 AM
> That's an amazingly broad generalization, broad to the point of being
> dangerously wrong. It depends entirely on the size of the card -
> because it's determined by the reported CHS geometry of the card,

First - CHS geometry on a flash card if a fake.

Second - CHS geometry on post-1995 hard disks is a fake too.

Third - in Windows, if the disk is not registered in BIOS, then Windows assumes
S=63 and H=255, with a cylinder of 8MB. This is hard-coded to Windows Disk.sys
driver.

Fourth - if the hard disk is registered in BIOS as LBA, then again S=63 and
H=255.

--
Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
maxim@storagecraft.com
http://www.storagecraft.com

larwe
01-12-2006, 10:48 AM
> Ok, so does it make sense that the MBR, (ie. everything leading up to
> the LBR), is all 0's on my SD cards? The were formatted as FAT32 under
> Win XP. Shouldn't the MBR contain some non-0 data?

It doesn't match with what I tried when I tested it (see rest of this
thread) but others report the same behavior you mentioned.

What I got was a populated partition table with an AA55 boot signature
at the end (in the first sector).


Oh! One thing I forgot - Some USB card-reader devices don't give you
physical sector access to the disk; they appear, for want of a better
phrase, on int 24h/int 25h only - not on int 13h. On these devices, you
can't get at the MBR - all you see is the logical volume.

I don't know if they still make readers with this oddity.

larwe
01-12-2006, 10:55 AM
Maxim S. Shatskih wrote:

> First - CHS geometry on a flash card if a fake.
>
> Second - CHS geometry on post-1995 hard disks is a fake too.

This is true, but the reported numbers exist anyway and are visible at
the int 13h level if nowhere else.

> Third - in Windows, if the disk is not registered in BIOS, then Windows assumes
> S=63 and H=255, with a cylinder of 8MB. This is hard-coded to Windows Disk.sys

I presume that some USB devices support int 13h type access emulation
then. Also note that PCMCIA adapters (all that I have tested, anyway)
appear at the int 13h level. While I was developing my first DOS
filesystem many moons ago for digital picture frames, I had to use
PCMCIA adapters to check my work, because the USB adapters only gave me
int 25/26h level access.

I repeat: 63 sectors is a generalization and given that we are messing
with data storage devices, a dangerous one. Please don't let your
63-sector-track assumption stomp on my storage media - let me know what
embedded devices you are involved with building, so I can avoid them.

Are you telling me that if I have two partitions on a CF card, and I
accidentally damage the logical structure of the first one, Windows
will silently blow away the second partition if I reformat the card?
Marvelous.

Maxim S. Shatskih
01-12-2006, 11:14 AM
> All the USB adapters I've used - which is many different types - cause
> Windows to format cards with an MBR. As a matter of interest, the units
> I have also report as removable to Windows.

Yes, looked at Disk driver source - Windows supports MBR on removable media,
but uses only the very first valid partition off it.

--
Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
maxim@storagecraft.com
http://www.storagecraft.com

Maxim S. Shatskih
01-12-2006, 11:22 AM
> Are you telling me that if I have two partitions on a CF card, and I
> accidentally damage the logical structure of the first one, Windows
> will silently blow away the second partition if I reformat the card?

Windows cannot support more then 1 partition on removable media.

--
Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
maxim@storagecraft.com
http://www.storagecraft.com

Maxim S. Shatskih
01-12-2006, 12:19 PM
> > Windows does not support MBR and partitions on removable media,
>
> What about USB and Firewire disks?

First, I was mistaken. MBR is supported on removable media. But only 1
partition is used on it. Look at Disk driver source from the DDK (if you have
one).

Second, do not mix removable _media_ and removable _device_. USB and 1394 disks
are fixed-media removable devices. Iomega ZIP USB is a removable media (special
diskettes) removable device.

Most USB flash drives are emulating the removable media - the USB diskette
drive with the diskette glued to it forever. But some old USB flash drives
emulate the real USB-attached hard disk.

--
Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
maxim@storagecraft.com
http://www.storagecraft.com

Maxim S. Shatskih
01-12-2006, 12:20 PM
> I repeat: 63 sectors is a generalization and given that we are messing
> with data storage devices, a dangerous one. Please don't let your
> 63-sector-track assumption stomp on my storage media - let me know what
> embedded devices you are involved with building, so I can avoid them.

I'm speaking about what Windows uses. If the disk is not in BIOS - then yes,
H=255 S=63.

Anyway CHS are obsolete, and I think that all modern OSes will work fine the
the MBR disk where the next partition starts at physical sector 1 (not skipping
a track).

More so. Linux runs fine with complete junk in CHS values in MBR entries.

All IO requests to disk are long ago going using LBA, both int 13h and native
(using the OS's driver).

--
Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
maxim@storagecraft.com
http://www.storagecraft.com

alexbohner@yahoo.com
01-23-2006, 07:13 AM
Ok, so if the MBR is effectively obsolete on removable media under
Windows, which explains my findings, what is the best method for
determining the equivalent 'CHS' and partition information from the USB
disk?

Thanks,
Alex

Maxim S. Shatskih
01-23-2006, 07:57 AM
Once more - in Windows, if the disk is not registered in BIOS, then H=63
and S=255.
Also, if the disk is registered in BIOS as LBA, then once again - H=63 and
S=255 by definition.

CHS values are no more used for IO since around 2000, neither in Windows
nor in Linux. Their only use is to fill the obsolete MBR values. BTW, Linux
boots and works fine with nonsense CHS values in the MBR :-) it was Linux
kernel 2.2.

Why do you ever need CHS? It is absolutely sure that, for a flash drive,
they are fake, used only to satisfy MS-DOS. All real IO is done using LBA.

--
Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
maxim@storagecraft.com
http://www.storagecraft.com

<alexbohner@yahoo.com> wrote in message
news:1138021986.336106.86960@g43g2000cwa.googlegro ups.com...
> Ok, so if the MBR is effectively obsolete on removable media under
> Windows, which explains my findings, what is the best method for
> determining the equivalent 'CHS' and partition information from the USB
> disk?
>
> Thanks,
> Alex
>

alexbohner@yahoo.com
01-23-2006, 08:50 AM
Thanks for the clarification. I think I jumped the gun and didn't ask
the proper questions to resolve the problem.

- Assuming H=63 and S=255, will the calulated disk size always be
correct when using the cylinder count found with
IOCTL_DISK_GET_DRIVE_GEOMETRY?

- Can a RemovableMedia hold multiple partitions? If there is no MBR,
yet it works fine in both Linux and Windows machines I'm not sure how
this would be handled?

- Is it possible for a RemovableMedia device (or partition) to be set
active (bootable) like a fixed disk?

- Without an MBR, what is the proper method for reading the partition
information from the disk (eg, type, size, sectors, etc)?

Thanks,
Alex

Maxim S. Shatskih
01-23-2006, 09:28 PM
> - Assuming H=63 and S=255, will the calulated disk size always be
> correct when using the cylinder count found with
> IOCTL_DISK_GET_DRIVE_GEOMETRY?

A bit incorrect.

IOCTL_DISK_GET_PARTITION_INFO (sent to the single partition device, and not to
the whole disk) is a better way to get the disk size at the level below the
filesystem.

At filesystem level, you have GetDiskFreeSpaceEx (by name) and undocumented
NtQueryVolumeInformationFile/FileFsSizeInformation (by handle). In fact,
GetDiskFreeSpaceEx depends upon NtQueryVolumeInformationFile internally.

> - Can a RemovableMedia hold multiple partitions?

Yes, but Windows will ignore all but first :) if you have DDK - see the
comments in Disk driver source - they only create 1 partition device object for
removable media.

If the media has no MBR - then IoReadPartitionTable code below Disk will "cook"
the partition table information with 1 huge partition. This is called
"super-floppy".

If the media has MBR - then IoReadPartitionTable will really translate the MBR
to some Windows-defined structures (common to all possible on-disk PT layouts).

Then Disk will interpret the data from IoReadPartitionTable and create the
child device objects for partitions, and, for removable media, it will always
create 1.

Also the removable media devices cannot participate in software RAIDs, and
their drive letters (and volume mount points) depend only on their bus
connection position. For fixed disks, the partition drive letters depends on
MBR signature value and the partition start offset, and NOT on the bus
connection position of the drive.

Dunno on Linux.

> - Is it possible for a RemovableMedia device (or partition) to be set
> active (bootable) like a fixed disk?

Depends on BIOS mainly.

> - Without an MBR, what is the proper method for reading the partition
> information from the disk (eg, type, size, sectors, etc)?

Without an MBR, IoReadPartitionTable will respond "the disk has 1 partition of
the whole disk size". Then the Disk.sys driver will do the same.

Also IOCTL_SCSI_PASS_THROUGH with SCSIOP_READ_CAPACITY :) this is even below
the partition queries and work for all connection buses. It is mapped to
bus-specific hardware "get size" request. For SCSI, it is not mapped at all :)

Also for IDE/ATA/SATA only, you can read the 512 bytes of disk IDENTIFY data
(this is IOCTL_SCSI_MINIPORT/SMART_RCV_DRIVE_DATA, do not be confused with SCSI
and SMART here - this couple works from NT4 up for SMARTless IDE disks) - and
the UserAddressableSectors field (defined by ATA spec) is the disk size.

--
Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
maxim@storagecraft.com
http://www.storagecraft.com