>Synopsis:      Booting a kernel at 1.9TB on an MBR disk
>Category:      amd64
>Environment:
        System      : OpenBSD 6.0
        Details     : OpenBSD 6.0 (GENERIC.MP) #2319: Tue Jul 26 13:00:43 MDT 
2016
                         
[email protected]:/usr/src/sys/arch/amd64/compile/GENERIC.MP

        Architecture: OpenBSD.amd64
        Machine     : amd64
>Description:
        Let's say that I'm trying to boot an OpenBSD kernel which is
        located around 1.9TB on an MBR disk.  Just as an experiment.

        (Actually, it's a Windows machine with a 3TB hard-drive,
        unfortunately UEFI is buggy on this machine, so I'm keeping
        an MBR/BIOS setup, but then Windows can only use 2 TB.
        So I'd like to give 1.9 TB to Windows, and let OpenBSD use
        everything else (per the 'b *' trick in disklabel), so that
        the last terabyte can find some use.) 

        I know that doing this is not a really good idea, but still.
        If my BIOS can boot it, why not.

        bios(8) gets loaded by the BIOS, but then it can't find my 
        kernel.  I think some small changes to libsa/biosdev.c are
        enough to make this work. 

>How-To-Repeat:
        Put sd0a at a big enough sector, on an MBR disk.

        My setup is the following one:

---8<---

# disklabel sd0
# /dev/rsd0c:
type: SCSI
disk: SCSI disk
label: TOSHIBA DT01ACA3
duid: f1729af279036217
flags:
bytes/sector: 512
sectors/track: 63
tracks/cylinder: 255
sectors/cylinder: 16065
cylinders: 364801
total sectors: 5860533168
boundstart: 4067209216
boundend: 5860533168
drivedata: 0 

16 partitions:
#                size           offset  fstype [fsize bsize  cpg]
  a:          2103456       4067209216  4.2BSD   2048 16384    1 # /
  b:         17189573       4069312672    swap                   # none
  c:       5860533168                0  unused                   
  d:         25173824       4086502272  4.2BSD   2048 16384    1 # /tmp
  e:         27888832       4111676096  4.2BSD   2048 16384    1 # /var
  f:          4192960       4139564928  4.2BSD   2048 16384    1 # /usr
  g:          2216960       4143757888  4.2BSD   2048 16384    1 # /altroot
  h:         25157792       4145974848  4.2BSD   2048 16384    1 # /usr/local
  i:       4067207168             2048    NTFS                   
  j:          4192960       4171132640  4.2BSD   2048 16384    1 # /usr/src
  k:          4192992       4175325600  4.2BSD   2048 16384    1 # /usr/obj
  l:       1681014528       4179518592  4.2BSD   8192 65536    1 # /home

# fdisk -v sd0
fdisk: disk too large (5860533168 sectors). size truncated.
Primary GPT:
        Not Found

Secondary GPT:
        Not Found

MBR:
Disk: sd0       geometry: 267349/255/63 [4294961685 Sectors]
Offset: 0       Signature: 0xAA55
            Starting         Ending         LBA Info:
 #: id      C   H   S -      C   H   S [       start:        size ]
-------------------------------------------------------------------------------
*0: 07      0  32  33 - 253172  16  28 [        2048:  4067207168 ] NTFS
 1: A6 253172  16  29 - 267342  56  42 [  4067209216:   227643584 ] OpenBSD
 2: 00      0   0   0 -      0   0   0 [           0:           0 ] unused
 3: 00      0   0   0 -      0   0   0 [           0:           0 ] unused

# installboot -v sd0
Using / as root
installing bootstrap on /dev/rsd0c
using first-stage /usr/mdec/biosboot, second-stage /usr/mdec/boot
copying /usr/mdec/boot to /boot
/boot is 5 blocks x 16384 bytes
fs block shift 2; part offset 4067209216; inode block 40, offset 2728
master boot record (MBR) at sector 0
        partition 0: type 0x07 offset 2048 size 4067207168
        partition 1: type 0xA6 offset 4067209216 size 227643584
/usr/mdec/biosboot will be written at sector 4067209216
installboot: /usr/mdec/biosboot extends beyond sector 268435455. OpenBSD might 
not boot.

---8<---

        "OpenBSD might not boot", indeed, but not because of some
        BIOS limitation here, AFAICT.

        Installation works just fine, the bootloader appears but it
        won't find the kernel or anything on sd0a:

        >> OpenBSD/amd64 BOOT 3.31
        boot> boot
        cannot open hd0a:/etc/random.seed: Input/output error
        booting hd0a:/bsd: open hd0a:/bsd: Input/output error
          failed(5). will try /bsd 

>Fix:

        After a few trials and errors, it appears that it might just
        be a limitation of arch/amd64/stand/libsa/biosdev.c.

        In the following line, in biosstrategy(),

        blk += dip->disklabel.d_partitions[...].p_offset;

        ... for my particular case, blk gets a value of -227758064.
        blk is a daddr32_t value, which will overflow with a big
        enough p_offset.

        The following patch, which is very crude, lets me boot the
        kernel.  Some 32-bit values were just switched to 64 bits.


Index: boot/Makefile
===================================================================
RCS file: /cvs/src/sys/arch/amd64/stand/boot/Makefile,v
retrieving revision 1.31
diff -u -p -u -p -r1.31 Makefile
--- boot/Makefile       30 Mar 2016 06:38:45 -0000      1.31
+++ boot/Makefile       6 Oct 2016 17:32:52 -0000
@@ -44,7 +44,7 @@ SRCS+=        aes_xts.c explicit_bzero.c hmac_s
 .endif
 
 .PATH: ${S}/lib/libkern
-SRCS+= divdi3.c moddi3.c qdivrem.c
+SRCS+= divdi3.c moddi3.c qdivrem.c udivdi3.c umoddi3.c
 SRCS+= strlcpy.c
 
 .PATH: ${S}/lib/libz
Index: cdboot/Makefile
===================================================================
RCS file: /cvs/src/sys/arch/amd64/stand/cdboot/Makefile,v
retrieving revision 1.27
diff -u -p -u -p -r1.27 Makefile
--- cdboot/Makefile     2 Sep 2015 01:52:25 -0000       1.27
+++ cdboot/Makefile     6 Oct 2016 17:32:52 -0000
@@ -35,7 +35,7 @@ SRCS+=        ufs.c cd9660.c
 SRCS+= aes_xts.c explicit_bzero.c hmac_sha1.c pbkdf2.c rijndael.c sha1.c
 
 .PATH: ${S}/lib/libkern/arch/i386 ${S}/lib/libkern
-SRCS+= divdi3.c moddi3.c qdivrem.c
+SRCS+= divdi3.c moddi3.c qdivrem.c udivdi3.c umoddi3.c
 
 .PATH: ${S}/lib/libz
 SRCS+= adler32.c crc32.c inflate.c inftrees.c
Index: libsa/biosdev.c
===================================================================
RCS file: /cvs/src/sys/arch/amd64/stand/libsa/biosdev.c,v
retrieving revision 1.27
diff -u -p -u -p -r1.27 biosdev.c
--- libsa/biosdev.c     1 Oct 2015 20:28:12 -0000       1.27
+++ libsa/biosdev.c     6 Oct 2016 17:32:52 -0000
@@ -49,9 +49,9 @@ static const char *biosdisk_err(u_int);
 static int biosdisk_errno(u_int);
 
 int CHS_rw (int, int, int, int, int, int, void *);
-static int EDD_rw (int, int, u_int32_t, u_int32_t, void *);
+static int EDD_rw (int, int, u_int64_t, u_int32_t, void *);
 
-static int biosd_io(int, bios_diskinfo_t *, u_int, int, void *);
+static int biosd_io(int, bios_diskinfo_t *, unsigned long long, int, void *);
 static u_int findopenbsd(bios_diskinfo_t *, const char **);
 
 extern int debug;
@@ -210,7 +210,7 @@ CHS_rw(int rw, int dev, int cyl, int hea
 }
 
 static __inline int
-EDD_rw(int rw, int dev, u_int32_t daddr, u_int32_t nblk, void *buf)
+EDD_rw(int rw, int dev, u_int64_t daddr, u_int32_t nblk, void *buf)
 {
        int rv;
        volatile static struct EDD_CB cb;
@@ -242,7 +242,7 @@ EDD_rw(int rw, int dev, u_int32_t daddr,
  * Read given sector, handling retry/errors/etc.
  */
 int
-biosd_io(int rw, bios_diskinfo_t *bd, u_int off, int nsect, void *buf)
+biosd_io(int rw, bios_diskinfo_t *bd, unsigned long long off, int nsect, void 
*buf)
 {
        int dev = bd->bios_number;
        int j, error;
@@ -716,6 +716,7 @@ biosstrategy(void *devdata, int rw, dadd
        bios_diskinfo_t *bd = &dip->bios_info;
        u_int8_t error = 0;
        size_t nsect;
+       daddr_t blk64;
 
 #ifdef SOFTRAID
        /* Intercept strategy for softraid volumes. */
@@ -724,13 +725,13 @@ biosstrategy(void *devdata, int rw, dadd
 #endif
 
        nsect = (size + DEV_BSIZE - 1) / DEV_BSIZE;
-       blk += dip->disklabel.d_partitions[B_PARTITION(dip->bsddev)].p_offset;
+       blk64 = blk + 
dip->disklabel.d_partitions[B_PARTITION(dip->bsddev)].p_offset;
 
        /* Read all, sub-functions handle track boundaries */
-       if (blk < 0)
+       if (blk64 < 0)
                error = EINVAL;
        else
-               error = biosd_io(rw, bd, blk, nsect, buf);
+               error = biosd_io(rw, bd, blk64, nsect, buf);
 
 #ifdef BIOS_DEBUG
        if (debug) {


dmesg:
OpenBSD 6.0 (GENERIC.MP) #2319: Tue Jul 26 13:00:43 MDT 2016
    [email protected]:/usr/src/sys/arch/amd64/compile/GENERIC.MP
real mem = 8519057408 (8124MB)
avail mem = 8256397312 (7873MB)
mpath0 at root
scsibus0 at mpath0: 256 targets
mainbus0 at root
bios0 at mainbus0: SMBIOS rev. 2.7 @ 0xeb400 (17 entries)
bios0: vendor American Megatrends Inc. version "P1.60" date 07/11/2013
bios0: ASRock FM2A85X-ITX
acpi0 at bios0: rev 2
acpi0: sleep states S0 S3 S4 S5
acpi0: tables DSDT FACP APIC FPDT MCFG AAFT HPET BGRT SSDT SSDT CRAT
acpi0: wakeup devices PCE2(S4) PCE3(S4) PCE4(S4) PCE5(S4) PCE6(S4) PCE7(S4) 
SBAZ(S4) CIR_(S3) PS2K(S4) PS2M(S4) P0PC(S4) OHC1(S4) EHC1(S4) OHC2(S4) 
EHC2(S4) OHC3(S4) [...]
acpitimer0 at acpi0: 3579545 Hz, 32 bits
acpimadt0 at acpi0 addr 0xfee00000: PC-AT compat
cpu0 at mainbus0: apid 16 (boot processor)
cpu0: AMD A10-5800K APU with Radeon(tm) HD Graphics, 3793.52 MHz
cpu0: 
FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,CFLUSH,MMX,FXSR,SSE,SSE2,HTT,SSE3,PCLMUL,MWAIT,SSSE3,FMA3,CX16,SSE4.1,SSE4.2,POPCNT,AES,XSAVE,OSXSAVE,AVX,F16C,NXE,MMXX,FFXSR,PAGE1GB,LONG,LAHF,CMPLEG,SVM,EAPICSP,AMCR8,ABM,SSE4A,MASSE,3DNOWP,OSVW,IBS,XOP,SKINIT,WDT,FMA4,NODEID,TBM,TOPEXT,ITSC,BMI1
cpu0: 64KB 64b/line 2-way I-cache, 16KB 64b/line 4-way D-cache, 2MB 64b/line 
16-way L2 cache
cpu0: ITLB 48 4KB entries fully associative, 24 4MB entries fully associative
cpu0: DTLB 64 4KB entries fully associative, 64 4MB entries fully associative
cpu0: smt 0, core 0, package 0
mtrr: Pentium Pro MTRR support, 8 var ranges, 88 fixed ranges
cpu0: apic clock running at 99MHz
cpu0: mwait min=64, max=64, IBE
cpu1 at mainbus0: apid 17 (application processor)
cpu1: AMD A10-5800K APU with Radeon(tm) HD Graphics, 3793.11 MHz
cpu1: 
FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,CFLUSH,MMX,FXSR,SSE,SSE2,HTT,SSE3,PCLMUL,MWAIT,SSSE3,FMA3,CX16,SSE4.1,SSE4.2,POPCNT,AES,XSAVE,OSXSAVE,AVX,F16C,NXE,MMXX,FFXSR,PAGE1GB,LONG,LAHF,CMPLEG,SVM,EAPICSP,AMCR8,ABM,SSE4A,MASSE,3DNOWP,OSVW,IBS,XOP,SKINIT,WDT,FMA4,NODEID,TBM,TOPEXT,ITSC,BMI1
cpu1: 64KB 64b/line 2-way I-cache, 16KB 64b/line 4-way D-cache, 2MB 64b/line 
16-way L2 cache
cpu1: ITLB 48 4KB entries fully associative, 24 4MB entries fully associative
cpu1: DTLB 64 4KB entries fully associative, 64 4MB entries fully associative
cpu1: smt 0, core 1, package 0
cpu2 at mainbus0: apid 18 (application processor)
cpu2: AMD A10-5800K APU with Radeon(tm) HD Graphics, 3793.11 MHz
cpu2: 
FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,CFLUSH,MMX,FXSR,SSE,SSE2,HTT,SSE3,PCLMUL,MWAIT,SSSE3,FMA3,CX16,SSE4.1,SSE4.2,POPCNT,AES,XSAVE,OSXSAVE,AVX,F16C,NXE,MMXX,FFXSR,PAGE1GB,LONG,LAHF,CMPLEG,SVM,EAPICSP,AMCR8,ABM,SSE4A,MASSE,3DNOWP,OSVW,IBS,XOP,SKINIT,WDT,FMA4,NODEID,TBM,TOPEXT,ITSC,BMI1
cpu2: 64KB 64b/line 2-way I-cache, 16KB 64b/line 4-way D-cache, 2MB 64b/line 
16-way L2 cache
cpu2: ITLB 48 4KB entries fully associative, 24 4MB entries fully associative
cpu2: DTLB 64 4KB entries fully associative, 64 4MB entries fully associative
cpu2: smt 0, core 2, package 0
cpu3 at mainbus0: apid 19 (application processor)
cpu3: AMD A10-5800K APU with Radeon(tm) HD Graphics, 3793.11 MHz
cpu3: 
FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,CFLUSH,MMX,FXSR,SSE,SSE2,HTT,SSE3,PCLMUL,MWAIT,SSSE3,FMA3,CX16,SSE4.1,SSE4.2,POPCNT,AES,XSAVE,OSXSAVE,AVX,F16C,NXE,MMXX,FFXSR,PAGE1GB,LONG,LAHF,CMPLEG,SVM,EAPICSP,AMCR8,ABM,SSE4A,MASSE,3DNOWP,OSVW,IBS,XOP,SKINIT,WDT,FMA4,NODEID,TBM,TOPEXT,ITSC,BMI1
cpu3: 64KB 64b/line 2-way I-cache, 16KB 64b/line 4-way D-cache, 2MB 64b/line 
16-way L2 cache
cpu3: ITLB 48 4KB entries fully associative, 24 4MB entries fully associative
cpu3: DTLB 64 4KB entries fully associative, 64 4MB entries fully associative
cpu3: smt 0, core 3, package 0
ioapic0 at mainbus0: apid 5 pa 0xfec00000, version 21, 24 pins
acpimcfg0 at acpi0 addr 0xe0000000, bus 0-255
acpihpet0 at acpi0: 14318180 Hz
acpiprt0 at acpi0: bus 0 (PCI0)
acpiprt1 at acpi0: bus 1 (PCE2)
acpiprt2 at acpi0: bus -1 (PCE3)
acpiprt3 at acpi0: bus -1 (PCE4)
acpiprt4 at acpi0: bus -1 (PCE5)
acpiprt5 at acpi0: bus -1 (PCE6)
acpiprt6 at acpi0: bus -1 (PCE7)
acpiprt7 at acpi0: bus 3 (PE20)
acpiprt8 at acpi0: bus -1 (PE21)
acpiprt9 at acpi0: bus -1 (PE22)
acpiprt10 at acpi0: bus -1 (PE23)
acpicpu0 at acpi0: C2(0@100 io@0x1771), C1(@1 halt!), PSS
acpicpu1 at acpi0: C2(0@100 io@0x1771), C1(@1 halt!), PSS
acpicpu2 at acpi0: C2(0@100 io@0x1771), C1(@1 halt!), PSS
acpicpu3 at acpi0: C2(0@100 io@0x1771), C1(@1 halt!), PSS
"PNP0303" at acpi0 not configured
acpibtn0 at acpi0: PWRB
cpu0: 3793 MHz: speeds: 3800 3400 2900 2400 1900 1400 MHz
pci0 at mainbus0 bus 0
pchb0 at pci0 dev 0 function 0 "AMD AMD64 15/1xh Host" rev 0x00
ppb0 at pci0 dev 2 function 0 "AMD AMD64 15/1xh PCIE" rev 0x00: msi
pci1 at ppb0 bus 1
vga1 at pci1 dev 0 function 0 vendor "NVIDIA", unknown product 0x13c2 rev 0xa1
wsdisplay0 at vga1 mux 1: console (80x25, vt100 emulation)
wsdisplay0: screen 1-5 added (80x25, vt100 emulation)
azalia0 at pci1 dev 0 function 1 vendor "NVIDIA", unknown product 0x0fbb rev 
0xa1: msi
azalia0: no supported codecs
xhci0 at pci0 dev 16 function 0 "AMD Hudson-2 xHCI" rev 0x03: msi
usb0 at xhci0: USB revision 3.0
uhub0 at usb0 "AMD xHCI root hub" rev 3.00/1.00 addr 1
xhci1 at pci0 dev 16 function 1 "AMD Hudson-2 xHCI" rev 0x03: msi
usb1 at xhci1: USB revision 3.0
uhub1 at usb1 "AMD xHCI root hub" rev 3.00/1.00 addr 1
ahci0 at pci0 dev 17 function 0 "AMD Hudson-2 SATA" rev 0x40: msi, AHCI 1.3
ahci0: port 0: 6.0Gb/s
ahci0: port 1: 1.5Gb/s
scsibus1 at ahci0: 32 targets
sd0 at scsibus1 targ 0 lun 0: <ATA, TOSHIBA DT01ACA3, MX6O> SCSI3 0/direct 
fixed naa.5000039ff4e55432
sd0: 2861588MB, 512 bytes/sector, 5860533168 sectors
cd0 at scsibus1 targ 1 lun 0: <HL-DT-ST, BDDVDRW CH08LS10, 2.00> ATAPI 5/cdrom 
removable
ohci0 at pci0 dev 18 function 0 "AMD Hudson-2 USB" rev 0x11: apic 5 int 18, 
version 1.0, legacy support
ehci0 at pci0 dev 18 function 2 "AMD Hudson-2 USB2" rev 0x11: apic 5 int 17
usb2 at ehci0: USB revision 2.0
uhub2 at usb2 "AMD EHCI root hub" rev 2.00/1.00 addr 1
ohci1 at pci0 dev 19 function 0 "AMD Hudson-2 USB" rev 0x11: apic 5 int 18, 
version 1.0, legacy support
ehci1 at pci0 dev 19 function 2 "AMD Hudson-2 USB2" rev 0x11: apic 5 int 17
usb3 at ehci1: USB revision 2.0
uhub3 at usb3 "AMD EHCI root hub" rev 2.00/1.00 addr 1
piixpm0 at pci0 dev 20 function 0 "AMD Hudson-2 SMBus" rev 0x14: polling
iic0 at piixpm0
spdmem0 at iic0 addr 0x50: 4GB DDR3 SDRAM PC3-10600
spdmem1 at iic0 addr 0x51: 4GB DDR3 SDRAM PC3-10600
pciide0 at pci0 dev 20 function 1 "AMD Hudson-2 IDE" rev 0x00: DMA, channel 0 
configured to compatibility, channel 1 configured to compatibility
azalia1 at pci0 dev 20 function 2 "AMD Hudson-2 HD Audio" rev 0x01: apic 5 int 
16
azalia1: codecs: Realtek/0x0892
audio0 at azalia1
pcib0 at pci0 dev 20 function 3 "AMD Hudson-2 LPC" rev 0x11
ppb1 at pci0 dev 20 function 4 "AMD Hudson-2 PCI" rev 0x40
pci2 at ppb1 bus 2
ohci2 at pci0 dev 20 function 5 "AMD Hudson-2 USB" rev 0x11: apic 5 int 18, 
version 1.0, legacy support
ppb2 at pci0 dev 21 function 0 "AMD Hudson-2 PCIE" rev 0x00
pci3 at ppb2 bus 3
re0 at pci3 dev 0 function 0 "Realtek 8168" rev 0x06: RTL8168E/8111E-VL 
(0x2c80), msi, address XXX
rgephy0 at re0 phy 7: RTL8169S/8110S/8211 PHY, rev. 5
pchb1 at pci0 dev 24 function 0 "AMD AMD64 15/1xh Link Cfg" rev 0x00
pchb2 at pci0 dev 24 function 1 "AMD AMD64 15/1xh Address Map" rev 0x00
pchb3 at pci0 dev 24 function 2 "AMD AMD64 15/1xh DRAM Cfg" rev 0x00
km0 at pci0 dev 24 function 3 "AMD AMD64 15/1xh Misc Cfg" rev 0x00
pchb4 at pci0 dev 24 function 4 "AMD AMD64 15/1xh CPU Power" rev 0x00
pchb5 at pci0 dev 24 function 5 "AMD AMD64 15/1xh NB Power" rev 0x00
usb4 at ohci0: USB revision 1.0
uhub4 at usb4 "AMD OHCI root hub" rev 1.00/1.00 addr 1
usb5 at ohci1: USB revision 1.0
uhub5 at usb5 "AMD OHCI root hub" rev 1.00/1.00 addr 1
isa0 at pcib0
isadma0 at isa0
pckbc0 at isa0 port 0x60/5 irq 1 irq 12
pckbd0 at pckbc0 (kbd slot)
wskbd0 at pckbd0: console keyboard, using wsdisplay0
pcppi0 at isa0 port 0x61
spkr0 at pcppi0
wbsio0 at isa0 port 0x2e/2: NCT6776F rev 0x33
lm1 at wbsio0 port 0x290/8: NCT6776F
usb6 at ohci2: USB revision 1.0
uhub6 at usb6 "AMD OHCI root hub" rev 1.00/1.00 addr 1
uhidev0 at uhub5 port 3 configuration 1 interface 0 "Razer Razer Abyssus" rev 
2.00/2.00 addr 2
uhidev0: iclass 3/1
ums0 at uhidev0: 5 buttons, Z dir
wsmouse0 at ums0 mux 0
vscsi0 at root
scsibus2 at vscsi0: 256 targets
softraid0 at root
scsibus3 at softraid0: 256 targets
root on sd0a (f1729af279036217.a) swap on sd0b dump on sd0b

Reply via email to