Skip to contentSkip to conversation

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

PATCH Superseded Kautuk Consul 3 participants 5 messages Feb 22, 2024 – Apr 4, 2024

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 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 | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
slof/fs/packages/disk-label.fs +11 -1
index 661c6b0..e680847 100644
@@ -139,6 +139,13 @@ CONSTANT /gpt-part-entry
    block block-size read drop    \ read sector
 ;
 
+\ read sector to array "block" and return actual bytes read
+: read-sector-ret ( sector-number -- actual)
+   \ block-size is 0x200 on disks, 0x800 on cdrom drives
+   block-size * 0 seek drop      \ seek to sector
+   block block-size read    \ read sector
+;
+
 : (.part-entry) ( part-entry )
    cr ." part-entry>active:        " dup part-entry>active c@ .d
    cr ." part-entry>start-head:    " dup part-entry>start-head c@ .d
@@ -204,7 +211,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 +287,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 +361,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

Hi,

Quoted 104 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 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 | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
slof/fs/packages/disk-label.fs +11 -1
index 661c6b0..e680847 100644
@@ -139,6 +139,13 @@ CONSTANT /gpt-part-entry
    block block-size read drop    \ read sector
 ;
 
+\ read sector to array "block" and return actual bytes read
+: read-sector-ret ( sector-number -- actual)
+   \ block-size is 0x200 on disks, 0x800 on cdrom drives
+   block-size * 0 seek drop      \ seek to sector
+   block block-size read    \ read sector
+;
+
 : (.part-entry) ( part-entry )
    cr ." part-entry>active:        " dup part-entry>active c@ .d
    cr ." part-entry>start-head:    " dup part-entry>start-head c@ .d
@@ -204,7 +211,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 +287,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 +361,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

So how does the patch look ? Any comments ?

#2 lore raw

Hi,

Quoted 98 lines · Kautuk Consul
Quoted 96 lines

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 | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
slof/fs/packages/disk-label.fs +10 -1
index 661c6b0..e680847 100644
@@ -139,6 +139,13 @@ CONSTANT /gpt-part-entry
    block block-size read drop    \ read sector
 ;
 
+\ read sector to array "block" and return actual bytes read
+: read-sector-ret ( sector-number -- actual)
+   \ block-size is 0x200 on disks, 0x800 on cdrom drives
+   block-size * 0 seek drop      \ seek to sector
+   block block-size read    \ read sector
+;
+
 : (.part-entry) ( part-entry )
    cr ." part-entry>active:        " dup part-entry>active c@ .d
    cr ." part-entry>start-head:    " dup part-entry>start-head c@ .d
@@ -204,7 +211,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 +287,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 +361,7 @@ CONSTANT /gpt-part-entry
    no-mbr? IF drop FALSE EXIT THEN  \ read MBR and check for DOS disk-label magic
 
Quoted 8 lines
Quoted 8 lines
slof/fs/packages/disk-label.fs +1 -0
    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

So how does the patch look ? Any comments from anyone ?

#3 lore raw
Quoted 6 lines · Kautuk Consul

Hi,

Quoted 5 lines · Kautuk Consul
Quoted 3 lines

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 10 lines
Quoted 10 lines

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

Quoted 10 lines
slof/fs/packages/disk-label.fs +2 -0
index 661c6b0..e680847 100644
@@ -139,6 +139,13 @@ CONSTANT /gpt-part-entry
     block block-size read drop    \ read sector
  ;
 
+\ read sector to array "block" and return actual bytes read
+: read-sector-ret ( sector-number -- actual)

Please add a space between "actual" and ")". I'd maybe also rather say "actual-bytes" so that nobody expects "actual-sectors" here.

Quoted 4 lines
Quoted 4 lines

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

Quoted 4 lines
slof/fs/packages/disk-label.fs +4 -0
+   \ block-size is 0x200 on disks, 0x800 on cdrom drives
+   block-size * 0 seek drop      \ seek to sector
+   block block-size read    \ read sector
+;

Could we please avoid duplicating code here? "read-sector" could now simply be implemented via read-sector-ret instead:

\ 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 \ read sector
;

: read-sector ( sector-number -- )
read-sector-ret drop
;

Quoted 27 lines
Quoted 27 lines
Quoted 27 lines
slof/fs/packages/disk-label.fs +4 -1
  : (.part-entry) ( part-entry )
     cr ." part-entry>active:        " dup part-entry>active c@ .d
     cr ." part-entry>start-head:    " dup part-entry>start-head c@ .d
@@ -204,7 +211,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 +287,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 +361,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
 
Quoted 7 lines
Quoted 5 lines
Quoted 5 lines
slof/fs/packages/disk-label.fs +0 -0
     debug-disk-label? IF
        ." Found " dos-logical-partitions .d ." logical partitions" cr
Signature · 1 line
2.31.1

So how does the patch look ? Any comments from anyone ?

Sorry, your original patch somehow didn't make it to my Inbox (though it's visible on http://patchwork.ozlabs.org/project/slof/list/ so the problem is certainly on my receiving side), so I just learnt about this patch today.

Anyway, apart from the code duplication, it looks fine to me, so if you could fix that in a v2, that would be great!

Thomas

#4 lore raw
Quoted 3 lines · Thomas Huth

Sorry, your original patch somehow didn't make it to my Inbox (though it's visible on http://patchwork.ozlabs.org/project/slof/list/ so the problem is certainly on my receiving side), so I just learnt about this patch today.

Not so sure, there are/were some oddities with it, see - patworks did not pick any response expect from Kautuk:

https://patchwork.ozlabs.org/project/slof/patch/[email protected]/

Then I tweaked the mailman so may be it is fixed now. Thanks,

ps. I disagree about that "+COMP and -COMP" guy as it is not making anything "more informative" as it is hardly ever possible with SLOF but it is a different story :)