Detect other software using embedding area
RFC 1/1 Detect other software using embedding area
Every so often people report that booting Windows renders GRUB unbootable. This is usually because some Windows application writes to a sector in the "embedding area" (or boot track), so that application and GRUB end up fighting with each other on alternate boots. I've begun to gather information about these applications, as there don't seem to be very many of them, and I propose that we do something like the following to defend ourselves against problems and to try to avoid causing problems in our turn.
On the applications in question: some of them are "licence managers", and I'm unsympathetic towards these, since their use of the boot track is solely to try to enforce their proprietary licences in a way that can't easily be worked around by uninstallation tools, but the effect of this is also to impinge upon users' rights to back up and restore their system without unreasonable amounts of effort. However, some of the applications are backup and recovery utilities, and may well have a better justification for storing data outside a partition, although I can't easily tell exactly what they're doing.
When I blogged about this recently (and rather unexpectedly ended up on Slashdot), several people followed up to say that GRUB shouldn't be using the embedding area because it was never defined to be used for anything in particular and has no protocol for arbitrating among conflicts like this. Sometimes I even replied to them, depending on how polite they were about it ... They're not entirely wrong; GRUB is skating close to the edge of the lack-of-specification here. However, all the alternatives have severe problems too, and you get to choose what set of things you'd like to break:
* Simply loading core.img from a filesystem using blocklists has obvious problems when the filesystem decides to rearrange blocks during fsck, defragmentation, etc.
* Most filesystems don't reserve enough space near the start for us to be able to insert core.img safely there (although we should make better use of the ones that do; but grub-devel is not a filesystem development list and there's only so much we can fix).
* Most people who suffer from problems like this are on dual-boot systems, where the free OS is not the first one to be installed, and in that situation it's not unusual for the BIOS to be unable to address disk space after that occupied by the first OS. Automatic partitioners can't safely move the first OS to make room for a boot partition at the start, so many people will in practice need GRUB to be placed before the first partition.
I'd like to avoid having another argument about that here. This patch is solely to try to work around the problems with our current setup in a way that makes as few changes as possible. Maybe in ten years we'll all be using GPT and won't have to worry about it.
Some people have also suggested that GRUB should use an error-correcting code such as Reed-Solomon when embedding core.img. This is an interesting idea and it might be worth doing in the future. However, it doesn't fully solve the problem at hand because GRUB would still be overwriting sectors used by these other applications, some of which are very popular and people actually do want to use. We won't do ourselves any favours by appearing to be a poor citizen.
2010-08-31 Colin Watson <[email protected]>
* util/i386/pc/grub-setup.c (embed_signatures): New array. (setup): When embedding the core image in a post-MBR gap, check for and avoid sectors matching any of the signatures in embed_signatures.
=== modified file 'util/i386/pc/grub-setup.c'
i386/pc/grub-setup.c +110 -3
@@ -63,6 +63,33 @@ static const grub_gpt_part_type_t grub_g#define grub_host_to_target32 (x) grub_cpu_to_le32(x)#define grub_host_to_target64 (x) grub_cpu_to_le64(x) +struct embed_signature +{ +const char *signature; +int signature_len; +}; + +/* Signatures of other software that may be using sectors in the embedding +area. */ +struct embed_signature embed_signatures[] = + { + { +/* ZISD */ + .signature ="ZISD" , + .signature_len =4 + }, + { +/* FlexNet */ + .signature ="\xd4\x41\xa0\xf5\x03\x00\x03\x00" , + .signature_len =8 + }, + { +/* FlexNet */ + .signature ="\xd8\x41\xa0\xf5\x02\x00\x02\x00" , + .signature_len =8 + } + }; + static void setup (const char *dir, const char *boot_file, const char *core_file,
@@ -85,7 +112,7 @@ setup (const char *dir,char *prefix =NULL ;char *tmp_img;int i; -grub_disk_addr_t first_sector; +grub_disk_addr_t first_sector, es;grub_uint16_t current_segment =GRUB_BOOT_MACHINE_KERNEL_SEG + (GRUB_DISK_SECTOR_SIZE >>4 );grub_uint16_t last_length =GRUB_DISK_SECTOR_SIZE ;
@@ -400,15 +427,95 @@ setup (const char *dir,assert (first_block->segment ==grub_host_to_target16 (GRUB_BOOT_MACHINE_KERNEL_SEG + (GRUB_DISK_SECTOR_SIZE >>4 ))); -/* Make sure that the second blocklist is a terminator. */ block = first_block -1 ; + +if (strcmp (dest_partmap,"msdos" ) ==0 ) + { +/* Check for software that is already using parts of the MBR gap. */ +char *embed_signature_check =xmalloc (GRUB_DISK_SECTOR_SIZE ); +grub_uint16_t extra_sectors =0 ; + +for (es = embed_region.start +1 ; es < embed_region.start + core_sectors; + es++) + { +struct grub_boot_blocklist *prev_block; +grub_uint64_t prev_start; +grub_uint16_t prev_len; +grub_uint16_t prev_segment; + +if (grub_disk_read (dest_dev->disk, es,0 ,GRUB_DISK_SECTOR_SIZE , + embed_signature_check)) +continue ; + +for (i =0 ; i <ARRAY_SIZE (embed_signatures); i++) +if (!memcmp (embed_signatures[i].signature, embed_signature_check, + embed_signatures[i].signature_len)) +break ; +if (i ==ARRAY_SIZE (embed_signatures)) +continue ; +grub_util_info ("sector %llu is already in use; avoiding it" , es); + extra_sectors++; + +/* Adjust the last blocklist, splitting it in two if necessary, to +avoid this sector. */ + prev_block = block +1 ; + prev_start =grub_target_to_host64 (prev_block->start); + prev_len =grub_target_to_host16 (prev_block->len); + prev_segment =grub_target_to_host16 (prev_block->segment); + +if (es == prev_start) + prev_block->start =grub_host_to_target64 (prev_start +1 ); +else + { + prev_block->len =grub_host_to_target16 (es - prev_start); + + block->start =grub_host_to_target64 (es +1 ); + block->len =grub_host_to_target16 + (prev_len - (es - prev_start)); + block->segment =grub_host_to_target16 + (prev_segment + + (GRUB_DISK_SECTOR_SIZE >>4 ) * (es - prev_start)); + + block--; +if (block->len) +grub_util_error (_ ("the post-MBR gap is too fragmented by sectors used by other software" )); + } + } +free (embed_signature_check); + +if ((unsigned long ) (core_sectors + extra_sectors) + > embed_region.end - embed_region.start) + { +grub_util_warn (_ ("Your embedding area is unusually small. core.img won't fit in it." )); + goto unable_to_embed; + } + } + +/* Make sure that the last blocklist is a terminator. */ block->start =0 ; block->len =0 ; block->segment =0 ;/* Write the core image onto the disk. */ -if (grub_disk_write (dest_dev->disk, embed_region.start,0 , core_size, core_img)) +if (grub_disk_write (dest_dev->disk, embed_region.start,0 , +GRUB_DISK_SECTOR_SIZE , core_img))grub_util_error ("%s" , grub_errmsg); + tmp_img = core_img +GRUB_DISK_SECTOR_SIZE ; +for (block = first_block; block->start; block--) + { +grub_uint64_t start; +size_t size; + + start =grub_target_to_host64 (block->start); + size =grub_target_to_host16 (block->len) <<GRUB_DISK_SECTOR_BITS ; +/* The last sector may be incomplete. */ +if (! (block +1 )->start && (core_size %GRUB_DISK_SECTOR_SIZE )) + size -=GRUB_DISK_SECTOR_SIZE - (core_size %GRUB_DISK_SECTOR_SIZE ); + +if (grub_disk_write (dest_dev->disk, start,0 , size, tmp_img)) +grub_util_error ("%s" , grub_errmsg); + tmp_img += size; + }/* FIXME: can this be skipped? */ *boot_drive =0xFF ;
Other comments welcome.
Thanks,
Signature · 1 line
Colin Watson [[email protected]]
Hi,
Quoted 2 lines · Colin Watson
[...] "embedding area" [...]
(setup): When embedding the core image in a post-MBR gap, [...]
Aka "hidden blocks" ?
Quoted 4 lines
several people followed up to say that GRUB shouldn't be using the embedding area because it was never defined to be used for anything in particular and has no protocol for arbitrating among conflicts like this.
A valid point. All participants invite trouble.
Quoted 2 lines
but grub-devel is not a filesystem development list and there's only so much we can fix).
Maybe one should use external boot media of which GRUB can claim complete ownership. I.e. an USB stick or a CD which hosts GRUB and a custom configuration.
This way, GRUB would have no shares in the embattled area at all.
Main development task would be to allow easy creation and manipulation of the boot media. As frontend of grub-mkrescue ?
Would ISO 9660 multi-session be suitable for changing GRUB configuration on CD ?
A new session would be added with a new set of volume descriptors (superblock) and a new directory tree. Plus new or changed files. Does GRUB know it has to "mount" the first track of the last session on CD/DVD/BD media ? (I assume it knows when it deals with MMC
media.)
Quoted 2 lines
We won't do ourselves
any favours by appearing to be a poor citizen.
How about GRUB offers to become the umpire of the playfield ?
I.e. to have on the external media:
- GRUB
- copies of the MBR and embedded area
of each involved operating system
- a feature which puts one of those copies onto hard disk according to menu choice of the user and then boots the copied MBR.
- a feature which copies found MBR and embedded area onto external media or remote systems.
I am not educated enough to know whether GRUB already allows to copy data from its boot media to disks.
Actually i have a gzpLinux CD with custom SSH setup which allows me to boot my test machine and to restore backups of MBR + "embedded area" remotely from my workstation.
This allowed me to switch between GRUB and boot loaders of FreeBSD and OpenSolaris before i found out how to make GRUB boot those other systems. (My GRUB is hosted by a Debian system.)
To have a complete GNU/Linux as umpire is appealing to me. So eventually GRUB development should consider to adopt an existing rescue system for that purpose.
This system could then update its own GRUB configuration. (It would be interesting to make it add a new session to its own boot CD :)).
Have a nice day :)
Thomas
Quoted 6 lines · Thomas Schmitt
Quoted 2 lines · Colin Watson
but grub-devel is not a filesystem development list and there's only so much we can fix).
Maybe one should use external boot media of
which GRUB can claim complete ownership.
Most people want their system to be able to boot using the hard disk alone. Obviously experts can set up something different, but it would be pretty hopeless to say that you need a writable CD or USB stick to boot your newly installed GNU/Linux system.
Obviously feel free to look into this separately, but I would ask that you please start a new thread if you wish to do so as it does not really solve the problem at hand, which is a real problem for many of our users.
Thanks,
Signature · 1 line
Colin Watson [[email protected]]
Quoted 6 lines · Thomas Schmitt
Maybe one should use external boot media of which GRUB can claim complete ownership. I.e. an USB stick or a CD which hosts GRUB and a custom configuration.
This way, GRUB would have no shares in the embattled area at all.
This way, GRUB would be unusable on rack-mounted servers in data centers. You can't add a USB stick or CD there, if all you have is remote serial console access...
-cpghost.
Signature · 1 line
Cordula's Web. http://www.cordula.ws/
Quoted 8 lines · C. P. Ghost
Quoted 6 lines · Thomas Schmitt
Maybe one should use external boot media of which GRUB can claim complete ownership. I.e. an USB stick or a CD which hosts GRUB and a custom configuration.
This way, GRUB would have no shares in the embattled area at all.
Quoted 4 lines
This way, GRUB would be unusable on rack-mounted servers in data centers. You can't add a USB stick or CD there, if all you have is remote serial console access...
On servers hopefully you are setting up a special partition for grub to use.
The embedding area sounds like it is trying to solve a problem with dual-boot systems where the first OS is already installed and cannot be moved.
Quoted 11 lines
-cpghost.
--
Cordula's Web. http://www.cordula.ws/_______________________________________________
Grub-devel mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/grub-devel
i386/pc/grub-setup.c static const grub_gpt_part_type_t grub_g · line 71
Quoted 7 lines · Colin Watson
i386/pc/grub-setup.c +6
-0+struct embed_signature +{ + const char *signature; + int signature_len; +}; +
It seems this cannot take care of cases where signature starts at some byte offset in a sector, isn't it?
Signature · 1 line
bvk.chaitanya
Quoted 9 lines · BVK Chaitanya
i386/pc/grub-setup.c static const grub_gpt_part_type_t grub_g · line 70
Quoted 5 lines · Colin Watson
i386/pc/grub-setup.c +5
-0+struct embed_signature +{ + const char *signature; + int signature_len; +};It seems this cannot take care of cases where signature starts at some byte offset in a sector, isn't it?
Correct. If I see such a signature in practice then I'll extend it. ;-)
Signature · 1 line
Colin Watson [[email protected]]
Hi,
Quoted 5 lines · Colin Watson
When I blogged about this recently (and rather unexpectedly ended up on Slashdot), several people followed up to say that GRUB shouldn't be using the embedding area because it was never defined to be used for anything in particular and has no protocol for arbitrating among conflicts like this.
Unfortunately, where the PC BIOS is concerned a lot of things are de facto standards, rather than actual standards; but I've always held a far simpler view: The MBR and the remaining sectors in the first track (before the start of the first partition) are reserved for the sole use of the "boot manager". If GRUB is the boot manager, then no OS (and no application running under any OS) has the right to touch these sectors; and the opposite is also true - e.g. if GRUB is not the MBR (e.g. GRUB is chain-loaded by something else) then GRUB has no right to touch any of these sectors.
If applications running on Windows are causing problems (overwriting something they have no right to touch), then the best solution would be a public web page (maybe in the GRUB wiki?) that explains the problem and lists which applications are buggy/broken crap...
Cheers,
Brendan
Quoted 10 lines · Brendan Trotter
Hi,
Quoted 6 lines · Colin Watson
When I blogged about this recently (and rather unexpectedly ended up on Slashdot), several people followed up to say that GRUB shouldn't be using the embedding area because it was never defined to be used for anything in particular and has no protocol for arbitrating among conflicts like this.
Quoted 11 lines
Unfortunately, where the PC BIOS is concerned a lot of things are de facto standards, rather than actual standards; but I've always held a far simpler view: The MBR and the remaining sectors in the first track (before the start of the first partition) are reserved for the sole use of the "boot manager". If GRUB is the boot manager, then no OS (and no application running under any OS) has the right to touch these sectors; and the opposite is also true - e.g. if GRUB is not the MBR (e.g. GRUB is chain-loaded by something else) then GRUB has no right to touch any of these sectors.
I completely agree with this point of view.
Quoted 6 lines
If applications running on Windows are causing problems (overwriting something they have no right to touch), then the best solution would be a public web page (maybe in the GRUB wiki?) that explains the problem and lists which applications are buggy/broken crap...
List of such software would be a good thing. However relying on it as the sole solution supposes that the user is concious and searches the real reason. However this expectation however logical it seems is completely irrealistic given our userbase.
Quoted 10 lines
Cheers,
Brendan
_______________________________________________
Grub-devel mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/grub-devel
Signature · 2 lines
Regards Vladimir 'φ-coder/phcoder' Serbinenko