Skip to contentSkip to conversation

slof/fs/packages/disk-label.fs: improve checking for DOS boot partitions

PATCH In review Kautuk Consul 3 participants 4 messages Mar 18, 2024 – Mar 27, 2024

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 slof/fs/packages/disk-label.fs: improve checking for DOS boot partitions

While testing with a qcow2 with a DOS boot partition it was found that
when we set the logical_block_size in the guest XML to >512 then the
boot would fail in the following interminable loop:
<SNIP>
Trying to load: from: /pci@800000020000000/scsi@3 ... virtioblk_transfer: Access beyond end of device!
virtioblk_transfer: Access beyond end of device!
virtioblk_transfer: Access beyond end of device!
virtioblk_transfer: Access beyond end of device!
virtioblk_transfer: Access beyond end of device!
virtioblk_transfer: Access beyond end of device!
virtioblk_transfer: Access beyond end of device!
virtioblk_transfer: Access beyond end of device!
virtioblk_transfer: Access beyond end of device!
virtioblk_transfer: Access beyond end of device!
virtioblk_transfer: Access beyond end of device!
virtioblk_transfer: Access beyond end of device!
virtioblk_transfer: Access beyond end of device!
virtioblk_transfer: Access beyond end of device!
virtioblk_transfer: Access beyond end of device!
virtioblk_transfer: Access beyond end of device!
virtioblk_transfer: Access beyond end of device!
</SNIP>

Change the count-dos-logical-partitions Forth subroutine and the Forth subroutines calling count-dos-logical-partitions to check for this access beyond end of device error.

After making the above changes, it fails properly with the correct error
message as follows:
<SNIP>
Trying to load: from: /pci@800000020000000/scsi@3 ... virtioblk_transfer: Access beyond end of device!
virtioblk_transfer: Access beyond end of device!
virtioblk_transfer: Access beyond end of device!
virtioblk_transfer: Access beyond end of device!
virtioblk_transfer: Access beyond end of device!
virtioblk_transfer: Access beyond end of device!
virtioblk_transfer: Access beyond end of device!
virtioblk_transfer: Access beyond end of device!
virtioblk_transfer: Access beyond end of device!
virtioblk_transfer: Access beyond end of device!

E3404: Not a bootable device!

E3407: Load failed

Type 'boot' and press return to continue booting the system. Type 'reset-all' and press return to reboot the system.

Ready!
0 >
</SNIP>

---
 slof/fs/packages/disk-label.fs | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
slof/fs/packages/disk-label.fs +12 -4
index 661c6b0..2630701 100644
@@ -132,11 +132,16 @@ CONSTANT /gpt-part-entry
    debug-disk-label? IF dup ." actual=" .d cr THEN
 ;
 
-\ read sector to array "block"
-: read-sector ( sector-number -- )
+\ read sector to array "block" and return actual bytes read
+: read-sector-ret ( sector-number -- actual-bytes )
    \ block-size is 0x200 on disks, 0x800 on cdrom drives
    block-size * 0 seek drop      \ seek to sector
-   block block-size read drop    \ read sector
+   block block-size read    \ read sector
+;
+
+\ read sector to array "block"
+: read-sector ( sector-number -- )
+   read-sector-ret drop
 ;
 
 : (.part-entry) ( part-entry )
@@ -204,7 +209,8 @@ CONSTANT /gpt-part-entry
          part-entry>sector-offset l@-le    ( current sector )
          dup to part-start to lpart-start  ( current )
          BEGIN
-            part-start read-sector          \ read EBR
+            part-start read-sector-ret          \ read EBR
+            block-size < IF UNLOOP 0 EXIT THEN
             1 partition>start-sector IF
                \ ." Logical Partition found at " part-start .d cr
                1+
@@ -279,6 +285,7 @@ CONSTANT /gpt-part-entry
    THEN
 
    count-dos-logical-partitions TO dos-logical-partitions
+   dos-logical-partitions 0= IF false EXIT THEN
 
    debug-disk-label? IF
       ." Found " dos-logical-partitions .d ." logical partitions" cr
@@ -352,6 +359,7 @@ CONSTANT /gpt-part-entry
    no-mbr? IF drop FALSE EXIT THEN  \ read MBR and check for DOS disk-label magic
 
    count-dos-logical-partitions TO dos-logical-partitions
+   dos-logical-partitions 0= IF 0 EXIT THEN
 
    debug-disk-label? IF
       ." Found " dos-logical-partitions .d ." logical partitions" cr
Signature · 1 line
2.31.1
#1 lore raw
Quoted 3 lines · Kautuk Consul

While testing with a qcow2 with a DOS boot partition it was found that when we set the logical_block_size in the guest XML to >512 then the boot would fail

...

Quoted 25 lines
slof/fs/packages/disk-label.fs +8 -3
index 661c6b0..2630701 100644
@@ -132,11 +132,16 @@ CONSTANT /gpt-part-entry
     debug-disk-label? IF dup ." actual=" .d cr THEN
  ;
 
-\ read sector to array "block"
-: read-sector ( sector-number -- )
+\ read sector to array "block" and return actual bytes read
+: read-sector-ret ( sector-number -- actual-bytes )
     \ block-size is 0x200 on disks, 0x800 on cdrom drives
     block-size * 0 seek drop      \ seek to sector
-   block block-size read drop    \ read sector
+   block block-size read    \ read sector
+;
+
+\ read sector to array "block"
+: read-sector ( sector-number -- )
+   read-sector-ret drop
  ;
 
  : (.part-entry) ( part-entry )

slof/fs/packages/disk-label.fs CONSTANT /gpt-part-entry · line 210

Quoted 2 lines
slof/fs/packages/disk-label.fs +0 -0
@@ -204,7 +209,8 @@ CONSTANT /gpt-part-entry
           part-entry>sector-offset l@-le    ( current sector )
           dup to part-start to lpart-start  ( current )

I just noticed that according to the stack comment above, there is a "current" item on the stack...

slof/fs/packages/disk-label.fs CONSTANT /gpt-part-entry · line 213

Quoted 4 lines
slof/fs/packages/disk-label.fs +2 -1
           BEGIN
-            part-start read-sector          \ read EBR
+            part-start read-sector-ret          \ read EBR
+            block-size < IF UNLOOP 0 EXIT THEN

... which doesn't get dropped here before the EXIT ? Is the stack still right after this function exited early?

Quoted 12 lines
slof/fs/packages/disk-label.fs +1 -0
              1 partition>start-sector IF
                 \ ." Logical Partition found at " part-start .d cr
                 1+
@@ -279,6 +285,7 @@ CONSTANT /gpt-part-entry
     THEN
 
     count-dos-logical-partitions TO dos-logical-partitions
+   dos-logical-partitions 0= IF false EXIT THEN
 
     debug-disk-label? IF
        ." Found " dos-logical-partitions .d ." logical partitions" cr

slof/fs/packages/disk-label.fs CONSTANT /gpt-part-entry · line 362

Quoted 4 lines
slof/fs/packages/disk-label.fs +1 -0
@@ -352,6 +359,7 @@ CONSTANT /gpt-part-entry
     no-mbr? IF drop FALSE EXIT THEN  \ read MBR and check for DOS disk-label magic
 
     count-dos-logical-partitions TO dos-logical-partitions
+   dos-logical-partitions 0= IF 0 EXIT THEN

Similar question here, what about the "addr" stack item? Shouldn't it be dropped first?

Thomas

PS: I'm still having trouble receiving your mail, I just discovered v2 on patchwork and downloaded it from there...

#2 lore raw

Hi,

Quoted 34 lines · Thomas Huth
Quoted 3 lines · Kautuk Consul

While testing with a qcow2 with a DOS boot partition it was found that when we set the logical_block_size in the guest XML to >512 then the boot would fail

...

Quoted 23 lines
slof/fs/packages/disk-label.fs +8 -3
index 661c6b0..2630701 100644
@@ -132,11 +132,16 @@ CONSTANT /gpt-part-entry
     debug-disk-label? IF dup ." actual=" .d cr THEN
  ;
-\ read sector to array "block"
-: read-sector ( sector-number -- )
+\ read sector to array "block" and return actual bytes read
+: read-sector-ret ( sector-number -- actual-bytes )
     \ block-size is 0x200 on disks, 0x800 on cdrom drives
     block-size * 0 seek drop      \ seek to sector
-   block block-size read drop    \ read sector
+   block block-size read    \ read sector
+;
+
+\ read sector to array "block"
+: read-sector ( sector-number -- )
+   read-sector-ret drop
  ;
  : (.part-entry) ( part-entry )

slof/fs/packages/disk-label.fs CONSTANT /gpt-part-entry · line 210

Quoted 2 lines
slof/fs/packages/disk-label.fs +0 -0
@@ -204,7 +209,8 @@ CONSTANT /gpt-part-entry
           part-entry>sector-offset l@-le    ( current sector )
           dup to part-start to lpart-start  ( current )

I just noticed that according to the stack comment above, there is a "current" item on the stack...

Quoted 7 lines

slof/fs/packages/disk-label.fs CONSTANT /gpt-part-entry · line 213

Quoted 4 lines
slof/fs/packages/disk-label.fs +2 -1
           BEGIN
-            part-start read-sector          \ read EBR
+            part-start read-sector-ret          \ read EBR
+            block-size < IF UNLOOP 0 EXIT THEN

... which doesn't get dropped here before the EXIT ? Is the stack still right after this function exited early?

Thanks for catching this. I didn't notice this as I sent this v2 in a hurry.

Quoted 17 lines
Quoted 11 lines
slof/fs/packages/disk-label.fs +1 -0
              1 partition>start-sector IF
                 \ ." Logical Partition found at " part-start .d cr
                 1+
@@ -279,6 +285,7 @@ CONSTANT /gpt-part-entry
     THEN
     count-dos-logical-partitions TO dos-logical-partitions
+   dos-logical-partitions 0= IF false EXIT THEN
     debug-disk-label? IF
        ." Found " dos-logical-partitions .d ." logical partitions" cr

slof/fs/packages/disk-label.fs CONSTANT /gpt-part-entry · line 361

Quoted 3 lines
slof/fs/packages/disk-label.fs +1 -0
@@ -352,6 +359,7 @@ CONSTANT /gpt-part-entry
     no-mbr? IF drop FALSE EXIT THEN  \ read MBR and check for DOS disk-label magic
     count-dos-logical-partitions TO dos-logical-partitions
+   dos-logical-partitions 0= IF 0 EXIT THEN

Similar question here, what about the "addr" stack item? Shouldn't it be dropped first?

Yes. Will take a look at this too. Thanks!
I will make these both changes and test them out before sending out a v3.

Quoted 6 lines

Thomas

PS: I'm still having trouble receiving your mail, I just discovered v2 on patchwork and downloaded it from there...

Okay I will check everything from my side to see if everything is in order.

Quoted 1 line