From 56f3d2696985c7824cae6befa254ed51547322fc Mon Sep 17 00:00:00 2001 From: apio Date: Mon, 10 Jul 2023 14:54:55 +0200 Subject: [PATCH] kernel+libluna: Fix the CRC32 algorithm and use it to verify the GPT header --- kernel/src/fs/GPT.cpp | 2 -- libluna/src/CRC32.cpp | 4 +--- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/kernel/src/fs/GPT.cpp b/kernel/src/fs/GPT.cpp index 713be6dd..9a5c1d78 100644 --- a/kernel/src/fs/GPT.cpp +++ b/kernel/src/fs/GPT.cpp @@ -29,7 +29,6 @@ namespace GPT check(header.reserved == 0); check(header.this_lba == 1); -#if 0 const u32 chksum = checksum_gpt(header); if (chksum != header.checksum) @@ -37,7 +36,6 @@ namespace GPT kwarnln("gpt: Header checksum does not match, %#.8x != %#.8x!", chksum, header.checksum); return false; } -#endif u64 partition_table_start = header.partition_table_lba * GPT_SECTOR_SIZE; diff --git a/libluna/src/CRC32.cpp b/libluna/src/CRC32.cpp index 31965739..d23ff684 100644 --- a/libluna/src/CRC32.cpp +++ b/libluna/src/CRC32.cpp @@ -35,15 +35,13 @@ static const u32 crc_table[] = { namespace CRC32 { - // FIXME: This shouldn't return 0xffffffff when there are 4 zeros in a row somewhere in the data, right? That's what - // happens to the GPT header. (other CRC32 implementations don't seem to have this problem) u32 checksum(const u8* data, usize size) { u32 crc = 0xffffffffu; for (usize i = 0; i < size; i++) { - const u32 index = (crc & data[i]) & 0xff; + const u32 index = (crc & 0xff) ^ data[i]; crc = (crc >> 8) ^ (crc_table[index]); }