All of lore.kernel.org
 help / color / mirror / Atom feed
* grub-mkrescue: Problem with MBR partition table at start of EFI partition
@ 2019-04-21 13:43 Thomas Schmitt
  2019-04-21 17:30 ` Vladimir 'phcoder' Serbinenko
  2019-04-24 20:32 ` Daniel Kiper
  0 siblings, 2 replies; 38+ messages in thread
From: Thomas Schmitt @ 2019-04-21 13:43 UTC (permalink / raw)
  To: grub-devel; +Cc: pelzflorian

Hi,

this is about how exactly to solve a diagnosed and worked-around problem.

Guix is one of the few distros which make their installation ISOs by
help of grub-mkrescue. The EFI boot manager of an old Macbook got stuck
when such an ISO was presented on USB stick. I.e. it not only did not
boot the ISO but showed no other EFI partitions either.

A Debian Live 9 ISO does not impose such a problem.
The same Guix ISO on DVD rather than USB stick works well, too.

The owner of the machine is Florian Pelz (Cc'ed). He characterizes it as:
  MacBook Pro (13-inch, Mid 2010)
MacOS Yosemite reports:
  Model Name:            MacBook Pro
  Model Identifier:      MacBookPro7,1
  Boot ROM Version:      MBP71.003F.B00
  SMC Version (system):  1.62f7

During a longish bug hunt it turned out that the decisive difference
is the first block of the FAT filesystem image which Debian creates by
mkfs.fat whereas grub-mkrescue creates it by mformat.

The mformat-made image has an MBR partition table entry which claims the
whole image as partition 1:

  Device            Boot Start   End Sectors  Size Id Type
  /mnt/iso/efi.img1 *        0  2879    2880  1.4M  1 FAT12

If this partition entry is zeroed, then the EFI boot manager works even
when the USB stick with this modified ISO is plugged in.

Actually it suffices to change the start LBA from 0 to 1, so that the
partition does not start by the partition table. (I count this as proof
of an endless loop in EFI while it is exploring something like
extended partitions. Strange is that the main MBR partition 1 of the
Debian ISO points to that main MBR, too. So it seems to harm only in
sub partition tables. We repacked the Guix ISO to MBR rather than GPT.
Still stuck.)

The EFI FAT image in Debian Live 9 has no partition table entry in block 0.

Production of the partition entry can be suppressed by mformat option -k.
But then block 0 of the result is not marked as MBR and also causes
program "file" to give out a very sparse characterization:

  DOS floppy 2880k

Here is the "file" report about an image made without -k:

  DOS/MBR boot sector, code offset 0x3c+2, OEM-ID "MTOO4018",
  sectors/cluster 2, root entries 240, sectors 5760 (volumes <=32 MB) ,
  sectors/FAT 9, sectors/track 36,
  serial number 0x6c528b1f, unlabeled, FAT (12 bit), followed by FAT

An image made by a modified grub-mkrescue with mformat option -k
worked well for Florian Pelz.

---------------------------------------------------------------------------

Questions:

1: Is there any use for the partition entry in the EFI partition of a
   grub-mkrescue ISO ?

2: Is there any use for the information which mformat does not insert
   if option -k is set ?

If the partition entry is not needed but the other MBR-like bytes are
needed:

3: Is there a good example in GRUB util sources about how to open a file,
   seek to its byte 446, and write 16 zero bytes starting there ?
   (I'd like to propose a patch for grub-mkrescue.c which zeros the
    partition entry directly after the mformat run.)

---------------------------------------------------------------------------


Have a nice day :)

Thomas



^ permalink raw reply	[flat|nested] 38+ messages in thread
* Re: grub-mkrescue: Problem with MBR partition table at start of EFI partition
@ 2019-05-11 18:15 Michael Schierl
  2019-05-11 19:42 ` Thomas Schmitt
  0 siblings, 1 reply; 38+ messages in thread
From: Michael Schierl @ 2019-05-11 18:15 UTC (permalink / raw)
  To: grub-devel; +Cc: scdbackup, pelzflorian

[Please cc: me since I am not subscribed to that list]


Hello,


I know this is getting off-topic, but since I am surprised how many
people here (have to) base their knowledge of booting Windows install
media from rumors and "advertising", I decided to bring in my knowledge
and personal experience about the Windows installer boot process.


>> > This is not an install ISO but a USB install medium.  The USB drive
>> > was written by the Windows Media Creation Tool.  Copying instead a
>> > Windows ISO created by the Windows Media Creation Tool to a USB drive
>> > does *not* yield a bootable USB drive; it does not even boot on other
>> > computers.
>>
>> This would bring the MS-World back in sync with my understanding of UEFI
>> specs and other info sources.
>>
>> But there are substantial rumors that the ISO boots from USB stick.
>> Microsoft is said to advertise it for DVD+R DL and USB stick.
>> (I could not get my hands on ISO or advertising yet ...)

Yes, any Windows install ISO starting from Windows Vista supports
installation from a USB stick. However, that does *not* mean that you
can use a tool like Unix's "dd" to write it to the drive and expect it
to boot (of course you can try, but it won't boot; valid for all Windows
up to latest Windows 10 Insider version). Instead, assuming that you
already have some Windows machine, you can either use the Media Creation
Tool to write the ISO to your USB, or you can use the command line
utilities included in the ISO itself.

On the other hand, in the Windows world this rarely causes any confusion
since no Windows version I've encountered so far contains native tools
to write an ISO (or any other image file) to a USB stick as Unix' "dd"
command would. Starting from Vista you can natively write ISO files to
optical media and since Windows 8 you can mount ISOs as a drive without
burning them, but that "just dd it to a USB key" feature of isohybrid is
mainly useful to people who already have some kind of Unix running.

In case you ever run into the situation to create a USB key from an
Windows install ISO file from another OS than Windows (and you want that
media to boot both on UEFI and CSM), you'd have to resort to third-party
tools like ms-sys that can replicate this installation process, or
chainload the Windows boot loader from another bootloader that supports
it (e.g. from Grub - to get this discussion back on-topic). Or if you
already have such a USB stick of any other Windows version (since
Vista), just mount it, delete all the files and copy all the files from
inside the ISO to it.

Another thing to keep in mind is that until (at least) Windows 7, the
Disk Management GUI refused to change partitioning of external
"removable" mass storage devices, so in case you ever "dd" a smaller ISO
to a larger stick, you would have to resort to third-party software or
the command-line "diskpart" command to regain your USB key's capacity
back. That may be another reason that dd-able ISOhybrid files are mostly
used for Linux installs (since Linux does not have that restriction) and
not used for Windows install media.

> I wonder how other people can get to the impression that their attempts
> succeeded. (Will have to wait for somebody who is willing to go on with
> experiments after perceived success.)

They probably did not use "dd" in the first place to get their ISO to
the USB stick.

>> I will continue testing the Windows 10 USB install medium:
>> /dev/sdb1  *     2048 15728639 15726592  7.5G  c W95 FAT32 (LBA)
>
> That's not an EFI System Partition.
> So i assume the USB stick boots by BIOS and x86 machine code in the MBR.
> Unless we believe in EFI firmware that hops on arbitrary partitions ...

I do not believe that EFI firmware hops on arbitrary partitions either.

On the other hand, all 64-bit EFI firmware versions I encountered so far
(to be honest, I believe all of them were "designed to run Windows"), do
EFI boot from the *first* MBR-style partition of an *external* drive
(e.g. USB), if the partition type is either 0x0B or 0x0C, the partition
is marked bootable, and it contains a FAT32 filesystem with an
/EFI/BOOT/BOOTX64.EFI file on it. I have encountered machines who do not
boot it if only one of these conditions is unmet (different partition
type, not marked bootable, not the first partition slot, or drive was
attached internally via SATA).

I do not know if this is specified anywhere, but if it did not work,
people would complain quickly. My own multiboot USB key (which uses GRUB
for chainloading multiple Linux ISOs via loopback.cfg) also uses that
exact disk layout, and I never had any machine where it could not boot.
(As it has a USB-OTG port, I can even load new ISOs to it from my
smartphone). On the other hand, if I partitioned the USB stick in GPT
mode and used an EFI System Partition as primary partition, Windows
would not assign a drive letter to it when I attach it, and older OSes
would not be able to read the stick at all. (I never heard that the
Windows installer "broke an USB stick", but I heard it multiple times
about Linux installers that used isohybrid. Getting rid of all
partitions always "cured" the USB stick again, but many Windows users
are unable to fix this themselves).


And that is exactly what happens when you "write" a Windows Installer
ISO to a USB key:

It will create a MBR-style partition table with one "bootable" 0x0C
entry spanning the entire device, formats it at FAT32 and copies all the
files in the ISO to it. (In case install.wim is larger than 4GB and the
media creation tool is recent enough, it will run SplitWim to create
install.swm, install2.swm etc. from it and put those on the device
instead). Afterwards bootsect.exe is executed, which writes a MBR and a
partition boot record so that booting in legacy mode works (this is not
required for booting in UEFI mode, but does not hurt either).

So, while the ISO used a UDF filesystem, the USB stick will use a FAT32
filesystem, and since bootmgr can read both, installation will succeed
in either case.

> I do not know how to verify on Windows if the boot used UEFI.  Shall I continue testing?

On a booted (installed) Windows, the easiest way is running msinfo32.exe
(which shows "BIOS Mode: UEFI" in case booted from UEFI, and won't show
it in case it is booted in legacy mode).


When you are in the installer (or any other Preinstallation Environment
or Recovery Environment), you'd have to go to the registry editor (in
case of a standard install media, press next to get to the second
install screen, press Shift+F10 to open a command prompt, and run
"regedit" from there) and check the value of

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\PEFirmwareType

It is 1 for Legacy mode and 2 for UEFI mode.


Another option would be to either damage the bootx64.efi file or the
MBR/PBR of the drive.

Also there are two versions of the Boot Configuration Database; the
Legacy one as \BOOT\BCD and the EFI one as \EFI\MICROSOFT\BOOT\BCD (this
is used by dual-arch ISOs so that in BIOS mode you can choose if you
want 32- or 64-bit, but in UEFI mode it will pick 64-bit automatically).
If you delete/rename one of these, the boot manager will stop with an
error instead of booting.


Regards,


Michael


^ permalink raw reply	[flat|nested] 38+ messages in thread

end of thread, other threads:[~2019-06-15  6:02 UTC | newest]

Thread overview: 38+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-04-21 13:43 grub-mkrescue: Problem with MBR partition table at start of EFI partition Thomas Schmitt
2019-04-21 17:30 ` Vladimir 'phcoder' Serbinenko
2019-04-21 19:29   ` Thomas Schmitt
2019-04-24 20:32 ` Daniel Kiper
2019-04-25  6:00   ` pelzflorian (Florian Pelz)
2019-04-25  8:18   ` Thomas Schmitt
2019-04-25  9:36     ` pelzflorian (Florian Pelz)
2019-04-30 23:42     ` Vladimir 'phcoder' Serbinenko
2019-05-01  7:33       ` Thomas Schmitt
2019-05-09 20:21         ` Chris Murphy
2019-05-09 21:21           ` Thomas Schmitt
2019-05-10  6:21             ` Thomas Schmitt
2019-05-10  7:09               ` Thomas Schmitt
2019-05-10 12:12                 ` pelzflorian (Florian Pelz)
2019-05-10 13:46                   ` Thomas Schmitt
2019-05-10 16:12                     ` pelzflorian (Florian Pelz)
2019-05-10 16:27                       ` Thomas Schmitt
2019-05-11 10:51                         ` pelzflorian (Florian Pelz)
2019-05-11 12:05                           ` Thomas Schmitt
2019-05-11 14:20                             ` pelzflorian (Florian Pelz)
2019-05-11 17:31                               ` Thomas Schmitt
2019-05-11 19:13                                 ` pelzflorian (Florian Pelz)
2019-05-11 20:39                                   ` Thomas Schmitt
2019-05-13 21:04                                     ` Daniel Kiper
2019-05-13 21:55                                       ` Thomas Schmitt
2019-05-14  6:04                                       ` Thomas Schmitt
2019-05-15  9:45                                         ` Daniel Kiper
2019-05-15 10:57                                           ` Thomas Schmitt
2019-05-16 10:29                                             ` Daniel Kiper
2019-05-16 12:18                                               ` Thomas Schmitt
2019-05-20 12:35                                                 ` Daniel Kiper
2019-05-20 14:37                                                   ` Thomas Schmitt
2019-06-15  0:15                                                     ` Chris Murphy
2019-06-15  6:01                                                       ` pelzflorian (Florian Pelz)
2019-05-09 19:51       ` Chris Murphy
2019-05-09 21:06         ` Thomas Schmitt
2019-05-11 18:15 Michael Schierl
2019-05-11 19:42 ` Thomas Schmitt

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.