summaryrefslogtreecommitdiffstats
path: root/qemu/roms/openbios/libopenbios/ipchecksum.c
diff options
context:
space:
mode:
authorYang Zhang <yang.z.zhang@intel.com>2015-08-28 09:58:54 +0800
committerYang Zhang <yang.z.zhang@intel.com>2015-09-01 12:44:00 +0800
commite44e3482bdb4d0ebde2d8b41830ac2cdb07948fb (patch)
tree66b09f592c55df2878107a468a91d21506104d3f /qemu/roms/openbios/libopenbios/ipchecksum.c
parent9ca8dbcc65cfc63d6f5ef3312a33184e1d726e00 (diff)
Add qemu 2.4.0
Change-Id: Ic99cbad4b61f8b127b7dc74d04576c0bcbaaf4f5 Signed-off-by: Yang Zhang <yang.z.zhang@intel.com>
Diffstat (limited to 'qemu/roms/openbios/libopenbios/ipchecksum.c')
-rw-r--r--qemu/roms/openbios/libopenbios/ipchecksum.c55
1 files changed, 55 insertions, 0 deletions
diff --git a/qemu/roms/openbios/libopenbios/ipchecksum.c b/qemu/roms/openbios/libopenbios/ipchecksum.c
new file mode 100644
index 000000000..83f39bcea
--- /dev/null
+++ b/qemu/roms/openbios/libopenbios/ipchecksum.c
@@ -0,0 +1,55 @@
+/* Taken from Etherboot */
+
+#include "libopenbios/ipchecksum.h"
+
+unsigned short ipchksum(const void *data, unsigned long length)
+{
+ unsigned long sum;
+ unsigned long i;
+ const unsigned char *ptr;
+ union {
+ unsigned char byte[2];
+ unsigned short word;
+ } u;
+
+ /* In the most straight forward way possible,
+ * compute an ip style checksum.
+ */
+ sum = 0;
+ ptr = data;
+ for(i = 0; i < length; i++) {
+ unsigned long value;
+ value = ptr[i];
+ if (i & 1) {
+ value <<= 8;
+ }
+ /* Add the new value */
+ sum += value;
+ /* Wrap around the carry */
+ if (sum > 0xFFFF) {
+ sum = (sum + (sum >> 16)) & 0xFFFF;
+ }
+ }
+ u.byte[0] = (unsigned char) sum;
+ u.byte[1] = (unsigned char) (sum >> 8);
+ return (unsigned short) ~u.word;
+}
+
+unsigned short add_ipchksums(unsigned long offset, unsigned short sum, unsigned short new)
+{
+ unsigned long checksum;
+ sum = ~sum & 0xFFFF;
+ new = ~new & 0xFFFF;
+ if (offset & 1) {
+ /* byte swap the sum if it came from an odd offset
+ * since the computation is endian independant this
+ * works.
+ */
+ new = (new << 8) | (new >> 8);
+ }
+ checksum = sum + new;
+ if (checksum > 0xFFFF) {
+ checksum -= 0xFFFF;
+ }
+ return (~checksum) & 0xFFFF;
+}