Skip to content

Commit 72fbdd1

Browse files
core: kernel: use XOR for checksum
Change checksum logic from sum to XOR in C implementation, matching the specification. Update transfer_list_update_checksum and verification functions accordingly. Signed-off-by: Harrison Mutai <[email protected]>
1 parent 61d3d2d commit 72fbdd1

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

core/kernel/transfer_list.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -292,16 +292,17 @@ struct transfer_list_entry *transfer_list_next(struct transfer_list_header *tl,
292292
/*******************************************************************************
293293
* Calculate the byte sum (modulo 256) of a transfer list.
294294
* @tl: Pointer to the transfer list.
295-
* Return byte sum of the transfer list.
295+
* Return byte-wise XOR of the transfer list.
296296
******************************************************************************/
297-
static uint8_t calc_byte_sum(const struct transfer_list_header *tl)
297+
static uint8_t calc_byte_xor(const struct transfer_list_header *tl)
298298
{
299299
uint8_t *b = (uint8_t *)tl;
300300
uint8_t cs = 0;
301301
size_t n = 0;
302302

303-
for (n = 0; n < tl->size; n++)
304-
cs += b[n];
303+
for (n = 0; n < tl->size; n++) {
304+
cs ^= b[n];
305+
}
305306

306307
return cs;
307308
}
@@ -313,15 +314,17 @@ static uint8_t calc_byte_sum(const struct transfer_list_header *tl)
313314
******************************************************************************/
314315
void transfer_list_update_checksum(struct transfer_list_header *tl)
315316
{
316-
uint8_t cs = 0;
317-
318-
if (!tl || !(tl->flags & TL_FLAGS_HAS_CHECKSUM))
317+
if (!tl || !(tl->flags & TL_FLAGS_HAS_CHECKSUM)) {
319318
return;
319+
}
320+
321+
/*
322+
* Zeroing the checksum is necessary because `calc_byte_xor` works on the
323+
* entire TL (including the checksum field).
324+
*/
325+
tl->checksum = 0;
326+
tl->checksum = calc_byte_xor(tl);
320327

321-
cs = calc_byte_sum(tl);
322-
cs -= tl->checksum;
323-
cs = 256 - cs;
324-
tl->checksum = cs;
325328
assert(transfer_list_verify_checksum(tl));
326329
}
327330

@@ -338,7 +341,7 @@ bool transfer_list_verify_checksum(const struct transfer_list_header *tl)
338341
if (!(tl->flags & TL_FLAGS_HAS_CHECKSUM))
339342
return true;
340343

341-
return !calc_byte_sum(tl);
344+
return !calc_byte_xor(tl);
342345
}
343346

344347
/*******************************************************************************

0 commit comments

Comments
 (0)