[kernel] Fix off-by-one error in RAM disk segment offset by ghaerr · Pull Request #2666 · ghaerr/elks (original) (raw)

Found by AI in #2646.

It took a while for me to understand why the RAM disk previously worked even with the off-by-one error (despite previous data integrity testing), but found that due to the particular modulo arithmetic and the MINIX filesystem involved, the 128th sector got swapped with the 0th sector and all subsequent sectors slid up by one, so no data errors occurred.

Verified by adding a few new debug statements and running the following script, which copied 124k of data to use multiple segments:

ramdisk /dev/rd0 make 132
mkfs /dev/rd0 132
mount -t minix /dev/rd0 /mnt
cp /bin/memopad /mnt
cmp /bin/memopad /mnt/memopad