Bug 207940 - [patch] sys/boot/efi/boot1 select boot partition
Summary: [patch] sys/boot/efi/boot1 select boot partition
Status: New
Alias: None
Product: Base System
Classification: Unclassified
Component: kern (show other bugs)
Version: CURRENT
Hardware: amd64 Any
: --- Affects Many People
Assignee: freebsd-bugs mailing list
URL:
Keywords: patch
Depends on:
Blocks:
 
Reported: 2016-03-13 02:54 UTC by Naomichi Nonaka
Modified: 2018-08-09 01:51 UTC (History)
9 users (show)

See Also:
op: mfc-stable10?


Attachments
patch for efi boot1 partition selection (2.76 KB, application/x-bzip)
2016-03-13 02:54 UTC, Naomichi Nonaka
no flags Details
boot1.c.diff (7.06 KB, patch)
2016-03-15 21:32 UTC, op
no flags Details | Diff
boot1 patch rev2 - add input timeout (7.72 KB, patch)
2016-03-16 10:58 UTC, Naomichi Nonaka
no flags Details | Diff
small change (9.69 KB, patch)
2016-04-18 05:22 UTC, Masachika ISHIZUKA
no flags Details | Diff
boot1.c patch rev3 (10.73 KB, patch)
2016-05-09 06:03 UTC, Naomichi Nonaka
no flags Details | Diff
boot1.c patch rev4 (10.75 KB, patch)
2016-05-13 07:14 UTC, Naomichi Nonaka
no flags Details | Diff
boot1.c patch rev4 for stable/11 r330813 and later (15.67 KB, patch)
2018-07-01 15:34 UTC, Tomoaki AOKI
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Naomichi Nonaka 2016-03-13 02:54:36 UTC
Created attachment 168070 [details]
patch for efi boot1 partition selection

Current efi boo1 allows boot from auto-selected (first match) partition only.
I write patch to allow boot-time partiton selection for boot1.efi.
Comment 1 Steven Hartland freebsd_committer 2016-03-13 12:35:43 UTC
Out of interest why would this be needed?

Also as rule upload the patch without compression it and flag it as a patch as that makes it easer to quickly review.
Comment 2 Naomichi Nonaka 2016-03-14 02:44:57 UTC
(In reply to Steven Hartland from comment #1)

Sorry for inadequate format. It's my first PR and I couldn't find these rules in Web page. I'll do in next time.

I wrote this patch to select booting 10.3 or 11 which are installed on different partitions in one HD. It's useful for me.
Comment 3 op 2016-03-15 21:32:10 UTC
Created attachment 168264 [details]
boot1.c.diff

Decompress the originally attached patch.
Comment 4 op 2016-03-15 21:36:10 UTC
Add this PR as dependency to https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=203349 ?
Comment 5 Naomichi Nonaka 2016-03-16 10:58:38 UTC
Created attachment 168291 [details]
boot1 patch rev2
 - add input timeout
Comment 6 Naomichi Nonaka 2016-03-16 11:09:21 UTC
I created patch rev2 which has input timeout functionality.

So, now it works as original if there are no key in.

I'm not English native tonge. It's helpful if someone check
this patch's displaying messages.
Comment 7 Tomoaki AOKI 2016-03-16 13:28:28 UTC
(In reply to Steven Hartland from comment #1)

Comparing with MBR ones, current in-tree version (you fixed recently) corresponds to boot0 (512 bytes version), while Naomichi's first version corresponds to boot0ext (1024 bytes version). And Naomichi's second version is the combined one.

If someone using boot0ext wants to move to UEFI(GPT) environment, Naomichi's one would help.
Comment 8 Tomoaki AOKI 2016-03-16 13:47:33 UTC
(In reply to Naomichi Nonaka from comment #6)

Tried, and working as expected for me.

ThinkPad T420 with 2 drives (second one is in UltraBay adapter),
each drive have one UFS partition and one ZFS pool.

The default matches current in-tree boot1.efi, either booted from internal
first drive or second drive in UltraBay.

 *Means that if I select first drive with UEFI firmware, ZFS pool in
  first drive is auto-selected, and that if I select second drive,
  ZFS pool in second drive is auto-selected.
Comment 9 Steven Hartland freebsd_committer 2016-03-16 14:11:25 UTC
(In reply to Tomoaki AOKI from comment #8)
That would work without this change.
Comment 10 Tomoaki AOKI 2016-03-16 14:28:20 UTC
(In reply to Steven Hartland from comment #9)

I know. :-)
I mean that rev.1 of Nomichi's patch always required manual-select of partition, without default and timeout, but in rev.2, default selection (compatible with already MFC'ed version) is added as #0 with timeout support.
Comment 11 Masachika ISHIZUKA 2016-04-11 00:39:38 UTC
I tested on 10.3-RELEASE. The patch can be applied without errors.
Original version of boot1.efi on an internal drive can boot from an external drive, if I connected an external drive. The rev2 patched version of boot1.efi on an internal drive can boot only from the same internal drive. I want to be able to boot from other drives.
Comment 12 Naomichi Nonaka 2016-04-11 02:07:06 UTC
(In reply to Masachika ISHIZUKA from comment #11)

Mr. ISHIZAKI, Thanks for testing.

I tried, but in my PC, both 10.3-boot1.efi and my rev2 boot1.efi in internal drive can boot to internal drive only.

If you want to boot from external drive, you can choose external drive in 
"UEFI boot drive selection" menu.

It is helpful for me if you could show details such as
 1. your external drive is MBR partition or GPT partition.
 2. what partitions are shown in rev2 patched boot1.efi.
  2a you can't see external drive in boot1.efi's partition list. or
  2b you see external drive in list, select it, but booted from internal
 3 your external drive is connected via USB or eSATA.

Regards,

Naomichi Nonaka
Comment 13 Masachika ISHIZUKA 2016-04-11 03:55:50 UTC
(In reply to Naomichi Nonaka from comment #12)
Thank you for your reply.
I think this problem seems to come from my PC's EUFI bug.

> 1. your external drive is MBR partition or GPT partition.

  GPT partitions.

> 2. what partitions are shown in rev2 patched boot1.efi.
>  2a you can't see external drive in boot1.efi's partition list. or
>  2b you see external drive in list, select it, but booted from internal

  Both of external and internal drive has only one partition of freebsd-boot each, select menu was not displayed.

% gpart show ada0 <--- internal drive
=>       34  500118125  ada0  GPT  (238G)
         34       2014        - free -  (1.0M)
       2048    1024000     1  efi  (500M)          <- EFI
    1026048      81920     2  ms-basic-data  (40M)
    1107968     262144     3  ms-reserved  (128M)
    1370112  127845242     4  ms-basic-data  (61G) <--- win10pro
  129215354       1158        - free -  (579K)
  129216512     931840     5  !de94bba4-06d1-4d40-a16a-bfd50179d6ac  (455M)
  130148352  121923584     6  ms-basic-data  (58G)  <--- win8.1pro
  252071936     954368     7  !de94bba4-06d1-4d40-a16a-bfd50179d6ac  (466M)
  253026304       2048        - free -  (1.0M)
  253028352    3807104     8  ms-basic-data  (1.8G)
  256835456        128     9  freebsd-boot  (64K) <- 10.3R boot
  256835584    8388608    10  freebsd-ufs  (4.0G) <- 10.3R /
  265224192   16777216    11  freebsd-swap  (8.0G)<- 10.3R swap
  282001408   16777216    12  freebsd-ufs  (8.0G) <- 10.3R /var
  298778624   16777216    13  freebsd-ufs  (8.0G) <- 10.3R /usr
  315555840   33554432    14  freebsd-ufs  (16G)  <- 10.3R /home
  349110272  134217728    15  freebsd-ufs  (64G)  <- 10.3R /usr/local
  483328000   16788144    16  !d3bfe2de-3daf-11df-ba40-e3a556d89593  (8.0G)
  500116144       2015        - free -  (1.0M)

% gpart show da0 <--- external USB drive
=>        34  3907029097  da0  GPT  (1.8T)
          34        1606       - free -  (803K)
        1640          88    2  freebsd-boot  (44K) <- 11.0-current boot
        1728   134217728    3  freebsd-ufs  (64G)  <- 11.0-current /
   134219456    16777216    4  freebsd-swap  (8.0G)<- 11.0 swap
   150996672  3756032459       - free -  (1.7T)

 /dev/da0p1 was EFI partition, but if it exist, my PC's UEFI menu has
broken, it may be bug on my PC. So 'gpart delete -i 1 da0' was excuted.

> 3 your external drive is connected via USB or eSATA.

  via USB

  I can remove win8.1pro and if I can select boot partitions for FreeBSD without setting bootme, I will install 11.0-current to internal drive.

  My PC's EUFI is too buggy and sometimes halt on startup DELL logo when changing the UEFI boot options. If it happens, I have to remove the battery and remove the SSD with opening the case of PC, I don't want to change UEFI options.
Comment 14 Naomichi Nonaka 2016-04-11 04:39:40 UTC
Thank you for quick reply.

> select menu was not displayed.

It means boot1 found only 1 bootable partition. I might think boot1 could not recognize external drive is "varid" because of lack of EFI partiotion.

Sorry, it may take some time to prepare test environment.

> I can remove win8.1pro and if I can select boot partitions for FreeBSD without setting bootme, I will install 11.0-current to internal drive.

I assume rev2 boot1 can do that. It's (almost) same configuration as my test PC.

I'm very happy if you try. But, unfortunately, there are too many variant and bugs in UEFI firm, I can't tell if it really work or not. sigh.
Comment 15 Naomichi Nonaka 2016-04-11 05:16:05 UTC
OK, I found my mistake.

I had written "set boot_disk_unit=0" in /boot/loader.conf. after removing that line. rev2 boot1 can boot to external drive.

# I wrote that line in very early stage of my testing and forgot that.

for boot drive choise, I found 10.2R boot1.efi boot to external drive. So I assume Mr. ISIZUKA had installed 10.2R to internal drive, and when upgraded to 10.3R, boot1.efi had not upgraded to 10.3R.

You can check size of boot1.efi. 
 10.2R is about 33K, 10.3R is about 74K, and rev2 is about 82K.

Summary:
 when both internal drive and external drive have bootable partition.
*10.2R boot1.efi
 -> autoboot to external drive

*10.3R boot1.efi
 -> autoboot to internal drive

*rev2 boot1.efi
 -> show partition list. can boot to either internal or external by keyin
    timeout boot to internal
Comment 16 Masachika ISHIZUKA 2016-04-11 06:29:56 UTC
(In reply to Naomichi Nonaka from comment #15)
Thank you for your report details.

% ls -l /mnt2/EFI/freebsd/boot1.efi
-rwxr-xr-x  1 ishizuka  j5p  33262 Sep 19  2014 /mnt2/EFI/freebsd/boot1.efi

My boot1.efi is very older than 10.3R.

By the way, ver2 boot1 it did not work cause was found in my PC. Again my PC of UEFI of the bug was due. Input waiting loop for 10 seconds is done at the moment, and keyboard input is not recognized. Then, timer and keyboard input is restored to within to change several times a UEFI option on my PC, now works properly. I'm sorry to you fuss.
The external UBS disk and internal disk is selectable by ver2  boot1 loader on internal disk.
Comment 17 Masachika ISHIZUKA 2016-04-18 05:22:53 UTC
Created attachment 169420 [details]
small change

I changed the following to ver2 patch.
(1) when push any key other than partition select number or retry of boot failure, timeout count is stopped.
(2) partition number is decimal with p, ie hd(a)-> hd(p10).
(3) partition size is shown, ie hd(p10) (100G).
(4) maximum partition number(NUM_DEV_LIST) for selection is 35.
(5) number on select menu is shown in both the left and right.

Actually it labels also wanted to display, but I have to give up because it is a little difficult going.
Comment 18 Naomichi Nonaka 2016-05-09 06:03:53 UTC
Created attachment 170131 [details]
boot1.c patch rev3
Comment 19 Naomichi Nonaka 2016-05-09 06:13:17 UTC
(In reply to Masachika ISHIZUKA from comment #17)

I just posted patch rev3. which
* merged Mr.ISHIZUKA's changes
* improve event handling. now no key input delay.
* changed UI so that
  * space key stops timeout
  * enter key means selecting autoboot (key 0)

I hope you try this. thanks.
Comment 20 Masachika ISHIZUKA 2016-05-09 11:48:04 UTC
(In reply to Naomichi Nonaka from comment #19)
Thank you for ver3 patch.

Although this is almost good for me, I have some minor comments.

>   * enter key means selecting autoboot (key 0)
line number 521 should be "if (c == '\r') {".

And line number 550 should be "move_to_tol();" to prevent from "Select from 0 to b. Timeout in 10 seconds, [Space] to pause : Select from 0 to b. Timeout in 10 seconds, [Space] to pause : " when I pressed 'z'.
Comment 21 Naomichi Nonaka 2016-05-13 07:14:27 UTC
Created attachment 170241 [details]
boot1.c patch rev4

Bug fix. Thanks to Mr.ISHIZUKA.

Naomichi Nonaka
Comment 22 Masachika ISHIZUKA 2016-05-13 07:46:10 UTC
(In reply to Naomichi Nonaka from comment #21)
Thank you for ver4 patch.

It works fine.
Comment 23 nicolas 2018-07-01 14:43:02 UTC
The patch can be applied on FreeBSD 11.1-RELEASE but not anymore on FreeBSD 11.2-RELEASE due to big change in boot1.c.

For exemple, hd->PartitionNumber didn't exist anymore in boot1.c of FreeBSD 11.2-RELEASE.

Anyone can make this patch working on 11.2-RELEASE ?

Many thanks
Comment 24 nicolas 2018-07-01 14:49:13 UTC
See https://svnweb.freebsd.org/changeset/base/329114 for big changes done between 11.1-RELEASE and 11.2-RELEASE.
Comment 25 Tomoaki AOKI 2018-07-01 15:34:21 UTC
Created attachment 194818 [details]
boot1.c patch rev4 for stable/11 r330813 and later

Quick and ugly hack by mainly reviving deleted-but-needed-for-patch codes.
Should be applicable to 11.2, as releng/11.2 branched after r330813 (at r334459).
I had not enough time to dig into code changes to be tracked. :-(
Comment 26 nicolas 2018-07-01 16:18:45 UTC
Many thanks for the updated patch. It apply without any issue on FreeBSD 11.2-RELEASE.

On FreeBSD 11.1, it was easy to recompile only boot1 with the following commands:

# cd /usr/src/sys/boot/efi/boot1
<apply patch>
# make

It compile fine and generate both boot1.efi & boot1.efifat binary.

But on FreeBSD 11.2:

# cd /usr/src/stand/efi/boot1
<apply patch>
# make
Killed.
pid 1026 (make), uid 0, was killed: out of swap space

The machine have 6 Gb of free memory before running make. Running make eat all available memory.

How can I fix that ? What the best method to recompile only boot1.efi & boot1.efifat in a standalone way without recompile a full world ?

Many thanks
Comment 27 Kyle Evans freebsd_committer 2018-08-09 01:51:43 UTC
(In reply to nicolas from comment #26)

Hi,

cd /usr/src/stand && make -j4 all

in stable/11, after 11.1 but before 11.2. It still recompiles libsa and other bits, but the stand/ build is completely self-contained nowadays (assuming you have the proper toolchain to build for the target platform, of course. =))