Skip to contentSkip to conversation

compute checksum for roms bigger than a segment

PATCH In review Glauber Costa 3 participants 6 messages Mar 23, 2009 – Mar 24, 2009

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 compute checksum for roms bigger than a segment

Some option roms (e1000 provided by gpxe project as an example) are bigger than a segment. The current algorithm to compute the checksum fails in such case. To proper compute the checksum, this patch deals with the possibility of the rom's size crossing a segment border.

We don't need to worry about it crossing more than one segment border, since the option roms format only save one byte to store the image size (thus, maximum size = 0xff = 128k = 2 segments)

---
 bios/rombios.c |   33 +++++++++++++++++++++++++++------
 1 files changed, 27 insertions(+), 6 deletions(-)
bios/rombios.c +27 -6
index bc43251..6fede17 100644
@@ -10162,22 +10162,43 @@ no_serial:
   ret
 
 rom_checksum:
-  push ax
-  push bx
-  push cx
+  pusha
+  push ds
+
   xor  ax, ax
   xor  bx, bx
   xor  cx, cx
+  xor  dx, dx
+
   mov  ch, [2]
   shl  cx, #1
+
+  jnc checksum_loop
+  mov  dx, cx
+  mov  cx, #0xffff
+
 checksum_loop:
   add  al, [bx]
   inc  bx
   loop checksum_loop
+
+  cmp  dx, #0
+  je checksum_out
+
+  add  al, [bx]
+  mov  cx, dx
+  mov  dx, ds
+  add  dx, #0x1000
+  mov  ds, dx
+  xor  dx, dx 
+  xor  bx, bx
+
+  jmp  checksum_loop
+
+checksum_out:
   and  al, #0xff
-  pop  cx
-  pop  bx
-  pop  ax
+  pop  ds
+  popa 
   ret
 
 
Signature · 1 line
1.6.1.2
#1 lore raw
Quoted 30 lines · Glauber Costa

Some option roms (e1000 provided by gpxe project as an example) are bigger than a segment. The current algorithm to compute the checksum fails in such case. To proper compute the checksum, this patch deals with the possibility of the rom's size crossing a segment border.

We don't need to worry about it crossing more than one segment border, since the option roms format only save one byte to store the image size (thus, maximum size = 0xff = 128k = 2 segments)

---
 bios/rombios.c |   33 +++++++++++++++++++++++++++------
 1 files changed, 27 insertions(+), 6 deletions(-)
bios/rombios.c +3 -3
index bc43251..6fede17 100644
@@ -10162,22 +10162,43 @@ no_serial:
   ret
 
 rom_checksum:
-  push ax
-  push bx
-  push cx
+  pusha
+  push ds
+
   xor  ax, ax

bios/rombios.c no_serial: · line 10178

Quoted 10 lines
bios/rombios.c +6 -0
   xor  bx, bx
   xor  cx, cx
+  xor  dx, dx
+
   mov  ch, [2]
   shl  cx, #1
+
+  jnc checksum_loop
+  mov  dx, cx
+  mov  cx, #0xffff

xchg dx, cx
dec cx

bios/rombios.c no_serial: · line 10185

Quoted 7 lines
bios/rombios.c +3 -0
+
 checksum_loop:
   add  al, [bx]
   inc  bx
   loop checksum_loop
+
+  cmp  dx, #0

test dx, dx

bios/rombios.c no_serial: · line 10188

Quoted 3 lines
bios/rombios.c +3 -0
+  je checksum_out
+
+  add  al, [bx]

DS:[0] is added twice, intended?

bios/rombios.c no_serial: · line 10191

Quoted 3 lines
bios/rombios.c +3 -0
+  mov  cx, dx
+  mov  dx, ds
+  add  dx, #0x1000
     add  dh, #0x10
Quoted 17 lines
bios/rombios.c +9 -3
+  mov  ds, dx
+  xor  dx, dx 
+  xor  bx, bx
+
+  jmp  checksum_loop
+
+checksum_out:
   and  al, #0xff
-  pop  cx
-  pop  bx
-  pop  ax
+  pop  ds
+  popa 
   ret
 
 
Signature · 1 line
mailto:[email protected]
#2 lore raw
Quoted 17 lines · malc
Quoted 11 lines · Glauber Costa

   xor  ax, ax
   xor  bx, bx
   xor  cx, cx
+  xor  dx, dx
+
   mov  ch, [2]
   shl  cx, #1
+
+  jnc checksum_loop
+  mov  dx, cx
+  mov  cx, #0xffff

    xchg dx, cx
    dec  cx

Quoted 9 lines
Quoted 7 lines

+
 checksum_loop:
   add  al, [bx]
   inc  bx
   loop checksum_loop
+
+  cmp  dx, #0

    test dx, dx

ok.

Quoted 6 lines
Quoted 4 lines

+  je checksum_out
+
+  add  al, [bx]

    DS:[0] is added twice, intended?

it's not DS:[0]. It's the last byte from the previous segment that is not caught by the loop
Note it happens before cleaning bx and increasing cs. It works with it, does not without it.

Quoted 6 lines
Quoted 4 lines

+  mov  cx, dx
+  mov  dx, ds
+  add  dx, #0x1000

    add  dh, #0x10

Why is it preferred?

Signature · 5 lines
Glauber  Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#3 lore raw
Quoted 35 lines · Glauber Costa
Quoted 17 lines · malc
Quoted 11 lines · Glauber Costa

   xor  ax, ax
   xor  bx, bx
   xor  cx, cx
+  xor  dx, dx
+
   mov  ch, [2]
   shl  cx, #1
+
+  jnc checksum_loop
+  mov  dx, cx
+  mov  cx, #0xffff

    xchg dx, cx
    dec  cx

Quoted 9 lines
Quoted 7 lines

+
 checksum_loop:
   add  al, [bx]
   inc  bx
   loop checksum_loop
+
+  cmp  dx, #0

    test dx, dx

ok.

Quoted 5 lines
Quoted 4 lines

+  je checksum_out
+
+  add  al, [bx]

Quoted 5 lines
Quoted 1 line

    DS:[0] is added twice, intended?

it's not DS:[0]. It's the last byte from the previous segment that is not caught by the loop
Note it happens before cleaning bx and increasing cs. It works with it, does not without it.

Yes missed that.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

+  mov  cx, dx
+  mov  dx, ds
+  add  dx, #0x1000

    add  dh, #0x10

Why is it preferred?

Same reason test and xchg/dec were proposed - takes less space.

Signature · 1 line
mailto:[email protected]
#4 lore raw
In reply to malc
Quoted 37 lines · malc
Quoted 35 lines · Glauber Costa

Some option roms (e1000 provided by gpxe project as an example) are bigger than a segment. The current algorithm to compute the checksum fails in such case. To proper compute the checksum, this patch deals with the possibility of the rom's size crossing a segment border.

We don't need to worry about it crossing more than one segment border, since the option roms format only save one byte to store the image size (thus, maximum size = 0xff = 128k = 2 segments)

---
 bios/rombios.c |   33 +++++++++++++++++++++++++++------

 1 files changed, 27 insertions(+), 6 deletions(-)
bios/rombios.c +0 -0
index bc43251..6fede17 100644
@@ -10162,22 +10162,43 @@ no_serial:

   ret

 rom_checksum:
-  push ax
-  push bx
-  push cx
+  pusha
+  push ds
+
   xor  ax, ax
   xor  bx, bx
   xor  cx, cx
+  xor  dx, dx
+
   mov  ch, [2]

Quoted 8 lines
Quoted 5 lines

   shl  cx, #1
+
+  jnc checksum_loop
+  mov  dx, cx
+  mov  cx, #0xffff

    xchg dx, cx
    dec  cx

I actually skipped this one.

Ok for xchg, but cx at this time holds the remaining bytes, so dec cx won't have the effect you think it will.

Signature · 5 lines
Glauber  Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#5 lore raw
Quoted 46 lines · Glauber Costa
Quoted 37 lines · malc
Quoted 35 lines · Glauber Costa

Some option roms (e1000 provided by gpxe project as an example) are bigger than a segment. The current algorithm to compute the checksum fails in such case. To proper compute the checksum, this patch deals with the possibility of the rom's size crossing a segment border.

We don't need to worry about it crossing more than one segment border, since the option roms format only save one byte to store the image size (thus, maximum size = 0xff = 128k = 2 segments)

---
 bios/rombios.c |   33 +++++++++++++++++++++++++++------

 1 files changed, 27 insertions(+), 6 deletions(-)
bios/rombios.c +0 -0
index bc43251..6fede17 100644
@@ -10162,22 +10162,43 @@ no_serial:

   ret

 rom_checksum:
-  push ax
-  push bx
-  push cx
+  pusha
+  push ds
+
   xor  ax, ax
   xor  bx, bx
   xor  cx, cx
+  xor  dx, dx
+
   mov  ch, [2]

Quoted 8 lines
Quoted 5 lines

   shl  cx, #1
+
+  jnc checksum_loop
+  mov  dx, cx
+  mov  cx, #0xffff

    xchg dx, cx
    dec  cx

Quoted 4 lines

I actually skipped this one.

Ok for xchg, but cx at this time holds the remaining bytes, so dec cx won't have the effect you think it will.

Initially DX = 0, CX = remaining bytes
XCHG CX = 0, DX = remaining bytes
DEC CX = 0FFFFh

Unless i'm really missing something, this yields identical results with the original code.

Signature · 1 line
mailto:[email protected]