flashrom: Update test status to TEST_OK_PREW for ST M50FLW080A and SST49LF008A
[coreboot.git] / util / nvramtool / compute_ip_checksum.c
1 /*****************************************************************************\
2  * compute_ip_checksum.c
3  * $Id$
4 \*****************************************************************************/
5
6 #include <stdint.h>
7 #include "ip_checksum.h"
8
9 /* Note: The contents of this file were borrowed from the coreboot source
10  *       code which may be obtained from http://www.coreboot.org.
11  *       Specifically, this code was obtained from coreboot (LinuxBIOS)
12  *       version 1.0.0.8.
13  */
14
15 unsigned long compute_ip_checksum(void *addr, unsigned long length)
16 {
17         uint8_t *ptr;
18         volatile union {
19                 uint8_t  byte[2];
20                 uint16_t word;
21         } value;
22         unsigned long sum;
23         unsigned long i;
24         /* In the most straight forward way possible,
25          * compute an ip style checksum.
26          */
27         sum = 0;
28         ptr = addr;
29         for(i = 0; i < length; i++) {
30                 unsigned long value;
31                 value = ptr[i];
32                 if (i & 1) {
33                         value <<= 8;
34                 }
35                 /* Add the new value */
36                 sum += value;
37                 /* Wrap around the carry */
38                 if (sum > 0xFFFF) {
39                         sum = (sum + (sum >> 16)) & 0xFFFF;
40                 }
41         }
42         value.byte[0] = sum & 0xff;
43         value.byte[1] = (sum >> 8) & 0xff;
44         return (~value.word) & 0xFFFF;
45 }