vst_0201 wrote:Right, it was what I suspected it would be. I was avoiding that route as I prefer my initial attempt; however, I am sure I will find more benefit to the change more than just memory management (which is enough to convince).
I like your checksum description. Do you have code I could look at to get the feel for what you are doing? thanks
The checksum mechanism is done with several pieces of software.
Every binary of my OS has a basic header:
Code: Select all
begin: db 'SMN'
db 0 ; headerspec
dw 2 ; main-type: boot-files
dw 1 ; sub-type: bootloader
dd (end-begin) ; size
dd copyright
MD5 dd 0,0,0,0 ; MD5
checksum dd 0 ; Checksum 32-bit
times (40)+$$-$ db 0 ; Fill with zeroes
jmp codestart
codestart:
; code and data here
end:
Because of the header-data you can't jump the first byte. My entry-point for flat binaries is at offset 40, with a jump to codestart. I can make the header bigger without giving up an universal entrypoint for flat binaries.
In that header there is place for a MD5sum and checksum. I will use MD5 from the (more advanched) bootloader and the OS itself. When a simple checksum is needed and I want to use it with a small piece of code I use checksum (only used in bootfiles, in the OS itself MD5sums will used to self-check the integrity of files).
To calculate checksum, I wrote a small C-utility.
Code: Select all
int fd;
unsigned char *buffer; // !! allocate memory
unsigned int filesize; // !! open file (fd) and get size of file
unsigned int checksum;
unsigned int t,ofs;
/* Read file */
printf("Reading contents of file...");
buffer=malloc(filesize);
lseek(fd,0,SEEK_SET);
read(fd,buffer,filesize);
printf("OK\n");
/* Calculate checksum */
printf("Calculating checksum...");
checksum=0;
ofs=0;
for (t=(filesize-20); t>0; t--) {
checksum+=( ((int)buffer[ofs]>>(t & 15)) | ((int)buffer[ofs]<<(16-(t & 15))) ) & 0xffff;
ofs++;
if (ofs==16) ofs=36;
}
printf("OK (%X)\n",checksum);
Just a couple of fragments of my code because calculating the checksum is only one of the possibility's of the utility. Important is the checksum algoritm. I skip offsets 16 - 36 because that 20 bytes contain the MD5sum and the checksum itself. The checksum-calcultation looks like complicated, but in assembly it's much easier to understand.
I have 2 versions of the checksum check, both with 16-bit 8086 compatible code, because those checks are done before the check for a 386 cpu. If you know (or assume) you have a 386 or higher CPU, you can use 32-bit registers and make the code much simpler.
Very simple version for checking small binaries (max 64 kb):
Code: Select all
; ****************
; *** Checksum ***
; ****************
mov si,begin
xor bx,bx
xor dx,dx
cld
mov cx,(end-begin)-20
next_checksumS: cmp si,MD5
jne skip_checksums
add si,20
skip_checksums: lodsb
mov ah,0
push cx
and cl,15
ror ax,cl
add bx,ax
adc dx,0
pop cx
loop next_checksumS
cmp [checksum],bx
jne show_error_LE
cmp [checksum+2],dx
jne show_error_LE
A larger one, also 8086 compatible 16-bit code but with unlimited size (but of course limited by realmode memory access):
Code: Select all
CPU 8086
BITS 16
ORG 0h
begin: db 'SMN'
db 0 ; headerspec
dw 2 ; main-type: bootfiles
dw 1 ; sub-type
dd (end-begin) ; size
dd copyright
MD5 dd 0,0,0,0 ; MD5
checksum dd 0 ; Checksum 32-bit
times (40)+$$-$ db 0 ; Fill with zeroes
jmp codestart
%include "../checksum.inc"
; ******************
; *** Self check ***
; ******************
codestart: mov ax,0x1000
call Checksum
or ax,ax
jz ChecksumError
checksum.inc:
Code: Select all
; AX = segment
Checksum: push ax
push ds
xor si,si
push ax
mov ds,ax
xor bx,bx
xor dx,dx
cld
mov cx,[ds:8]
mov ax,[ds:10]
sub cx,20+16
sbb ax,0
push cx
push ax
mov ax,16
push ax
xor ax,ax
push ax
call Checksum_piece
pop ax
pop ax
mov si,4
mov ax,ds
inc ax
inc ax
mov ds,ax
call Checksum_piece
pop ax
pop ax
pop ds
mov ax,[ds:32]
mov cx,[ds:34]
pop ds
cmp ax,bx
jne ChecksumError
cmp cx,dx
jne ChecksumError
pop ax
ret
ChecksumError: pop ax
xor ax,ax
ret
Checksum_piece: push bp
mov bp,sp
Checksum_next: cmp si,16
jne CSinParagraph
xor si,si
mov ax,ds
inc ax
mov ds,ax
CSinParagraph: lodsb
mov ah,0
push cx
and cl,15
ror ax,cl
add bx,ax
adc dx,0
pop cx
dec cl
sub word [ss:bp+6],1
sbb word [ss:bp+4],0
cmp word [ss:bp+6],0
jne Checksum_next
cmp word [ss:bp+4],0
jne Checksum_next
pop bp
ret
The checksum-algorithm is based on rotations of bits. You can make it much simpler by only adding values of bytes. But with rotation of bytes, swapped bytes cause a checksum-error. The way you want to calculate your checksums is personal, you can choose every algorithm you want. This is just an example. I hope you can use these examples, because I cut away pieces of code to post only the checksum code here.
I added 450 kb's of random crap to my bootloader file, and even that large file, fragmented on a FAT12-filesystems, loads passing the checksum-test. So I know my files are loaded ok by the bootsector and FAT12-loader.
NOTE: I'm thinking about letting the first part of the header out of the checksum, so calculating can start at offset 36, after the checksum. That will make the routines much simpler..