--- /dev/null
- * Copyright 2007 H. Peter Anvin - All Rights Reserved
+/* ----------------------------------------------------------------------- *
+ *
++ * Copyright 2007-2008 H. Peter Anvin - All Rights Reserved
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom
+ * the Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall
+ * be included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * ----------------------------------------------------------------------- */
+
+/*
+ * syslinux/adv.h
+ */
+
+#ifndef _SYSLINUX_ADV_H
+#define _SYSLINUX_ADV_H
+
+#include <klibc/extern.h>
+#include <stddef.h>
+#include <syslinux/advconst.h>
+
+__extern void *__syslinux_adv_ptr;
+__extern size_t __syslinux_adv_size;
+
+static inline void *syslinux_adv_ptr(void)
+{
+ return __syslinux_adv_ptr;
+}
+
+static inline size_t syslinux_adv_size(void)
+{
+ return __syslinux_adv_size;
+}
+
+__extern int syslinux_adv_write(void);
+
+__extern int syslinux_setadv(int, size_t, const void *);
+__extern const void *syslinux_getadv(int, size_t *);
+
+#endif /* _SYSLINUX_ADV_H */
--- /dev/null
- * Copyright 2007 H. Peter Anvin - All Rights Reserved
+/* ----------------------------------------------------------------------- *
+ *
++ * Copyright 2007-2008 H. Peter Anvin - All Rights Reserved
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom
+ * the Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall
+ * be included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * ----------------------------------------------------------------------- */
+
+/*
+ * syslinux/advconst.h
+ *
+ * ADV defined constants
+ *
+ * Defined in a separate file so it can be used by non-COM32 code.
+ * Some of these constants are also defined in adv.inc, they better match...
+ */
+
+#ifndef _SYSLINUX_ADVCONST_H
+#define _SYSLINUX_ADVCONST_H
+
+#define ADV_END 0
+#define ADV_BOOTONCE 1
+
+#endif /* _SYSLINUX_ADVCONST_H */
--- /dev/null
- * Copyright 2007 H. Peter Anvin - All Rights Reserved
+/* ----------------------------------------------------------------------- *
+ *
++ * Copyright 2007-2008 H. Peter Anvin - All Rights Reserved
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom
+ * the Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall
+ * be included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * ----------------------------------------------------------------------- */
+
+/*
+ * syslinux/adv.c
+ *
+ * Access the syslinux auxilliary data vector
+ */
+
+#include <syslinux/adv.h>
+#include <klibc/compiler.h>
+#include <com32.h>
+
+void *__syslinux_adv_ptr;
+size_t __syslinux_adv_size;
+
+void __constructor __syslinux_get_adv(void)
+{
+ static com32sys_t reg;
+
+ reg.eax.w[0] = 0x001c;
+ __intcall(0x22, ®, ®);
+ __syslinux_adv_ptr = MK_PTR(reg.es, reg.ebx.w[0]);
+ __syslinux_adv_size = reg.ecx.w[0];
+}
--- /dev/null
- * Copyright 2007 H. Peter Anvin - All Rights Reserved
+/* ----------------------------------------------------------------------- *
+ *
++ * Copyright 2007-2008 H. Peter Anvin - All Rights Reserved
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom
+ * the Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall
+ * be included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * ----------------------------------------------------------------------- */
+
+/*
+ * syslinux/advwrite.c
+ *
+ * Write back the ADV
+ */
+
+#include <syslinux/adv.h>
+#include <klibc/compiler.h>
+#include <com32.h>
+
+int syslinux_adv_write(void)
+{
+ static com32sys_t reg;
+
+ reg.eax.w[0] = 0x001d;
+ __intcall(0x22, ®, ®);
+ return (reg.eflags.l & EFLAGS_CF) ? -1 : 0;
+}
--- /dev/null
- * Copyright 2007 H. Peter Anvin - All Rights Reserved
+/* ----------------------------------------------------------------------- *
+ *
++ * Copyright 2007-2008 H. Peter Anvin - All Rights Reserved
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom
+ * the Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall
+ * be included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * ----------------------------------------------------------------------- */
+
+/*
+ * syslinux/getadv.c
+ *
+ * Get a data item from the auxilliary data vector. Returns a pointer
+ * and sets *size on success; NULL on failure.
+ */
+
+#include <syslinux/adv.h>
+#include <klibc/compiler.h>
+#include <inttypes.h>
+
+const void *syslinux_getadv(int tag, size_t *size)
+{
+ const uint8_t *p;
+ size_t left, len;
+
+ p = syslinux_adv_ptr();
+ left = syslinux_adv_size();
+
+ while (left >= 2) {
+ uint8_t ptag = *p++;
+ size_t plen = *p++;
+ left -= 2;
+
+ if (ptag == ADV_END)
+ return NULL; /* Not found */
+
+ if (left < plen)
+ return NULL; /* Item overrun */
+
+ if (ptag == tag) {
+ *size = plen;
+ return p;
+ }
+
+ p += plen;
+ left -= plen;
+ }
+
+ return NULL;
+}
--- /dev/null
- * Copyright 2007 H. Peter Anvin - All Rights Reserved
+/* ----------------------------------------------------------------------- *
+ *
++ * Copyright 2007-2008 H. Peter Anvin - All Rights Reserved
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom
+ * the Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall
+ * be included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * ----------------------------------------------------------------------- */
+
+/*
+ * syslinux/setadv.c
+ *
+ * (Over)write a data item in the auxilliary data vector. To
+ * delete an item, set its length to zero.
+ *
+ * Return 0 on success, -1 on error, and set errno.
+ *
+ * NOTE: Data is not written to disk unless
+ * syslinux_adv_write() is called.
+ */
+
+#include <syslinux/adv.h>
+#include <klibc/compiler.h>
+#include <inttypes.h>
+#include <string.h>
+#include <errno.h>
+#include <alloca.h>
+
+int syslinux_setadv(int tag, size_t size, const void *data)
+{
+ uint8_t *p, *advtmp;
+ size_t left;
+
+ if ((unsigned)tag-1 > 254) {
+ errno = EINVAL;
+ return -1; /* Impossible tag value */
+ }
+
+ if (size > 255) {
+ errno = ENOSPC; /* Max 255 bytes for a data item */
+ return -1;
+ }
+
+ left = syslinux_adv_size();
+ p = advtmp = alloca(left);
+ memcpy(p, syslinux_adv_ptr(), left); /* Make working copy */
+
+ while (left >= 2) {
+ uint8_t ptag = p[0];
+ size_t plen = p[1]+2;
+
+ if (ptag == ADV_END)
+ break;
+
+ if (ptag == tag) {
+ /* Found our tag. Delete it. */
+
+ if (plen >= left) {
+ /* Entire remainder is our tag */
+ break;
+ }
+ memmove(p, p+plen, left-plen);
+ } else {
+ /* Not our tag */
+ if (plen > left)
+ break; /* Corrupt tag (overrun) - overwrite it */
+
+ left -= plen;
+ p += plen;
+ }
+ }
+
+ /* Now (p, left) reflects the position to write in and how much space
+ we have for our data. */
+
+ if (size) {
+ if (left < size+2) {
+ errno = ENOSPC; /* Not enough space for data */
+ return -1;
+ }
+
+ *p++ = tag;
+ *p++ = size;
+ memcpy(p, data, size);
+ left -= size+2;
+ }
+
+ memset(p, 0, left);
+
+ /* If we got here, everything went OK, commit the write to low memory */
+ memcpy(syslinux_adv_ptr(), advtmp, syslinux_adv_size());
+
+ return 0;
+}
--- /dev/null
- * Copyright 2007 H. Peter Anvin - All Rights Reserved
+/* ----------------------------------------------------------------------- *
+ *
++ * Copyright 2007-2008 H. Peter Anvin - All Rights Reserved
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, Inc., 53 Temple Place Ste 330,
+ * Boston MA 02111-1307, USA; either version 2 of the License, or
+ * (at your option) any later version; incorporated herein by reference.
+ *
+ * ----------------------------------------------------------------------- */
+
+/*
+ * advdump.c
+ *
+ * Dump the contents of the syslinux ADV
+ */
+
+#include <stdio.h>
+#include <console.h>
+#include <syslinux/adv.h>
+#include <string.h>
+
+int main(void)
+{
+ uint8_t *p, *ep;
+ size_t s = syslinux_adv_size();
+ char buf[256];
+
+ openconsole(&dev_stdcon_r, &dev_stdcon_w);
+
+ p = syslinux_adv_ptr();
+
+ printf("ADV size: %zd bytes at %p\n", s, p);
+
+ ep = p+s; /* Need at least opcode+len */
+ while (p < ep-1 && *p) {
+ int t = *p++;
+ int l = *p++;
+
+ if (p+l > ep)
+ break;
+
+ memcpy(buf, p, l);
+ buf[l] = '\0';
+
+ printf("ADV %3d: \"%s\"\n", t, buf);
+
+ p += l;
+ }
+
+ return 0;
+}
--- /dev/null
- * Copyright 2007 H. Peter Anvin - All Rights Reserved
+/* ----------------------------------------------------------------------- *
+ *
++ * Copyright 2007-2008 H. Peter Anvin - All Rights Reserved
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, Inc., 53 Temple Place Ste 330,
+ * Boston MA 02111-1307, USA; either version 2 of the License, or
+ * (at your option) any later version; incorporated herein by reference.
+ *
+ * ----------------------------------------------------------------------- */
+
+/*
+ * setadv.c
+ *
+ * (Over)write a data item in the auxilliary data vector. To
+ * delete an item, set its length to zero.
+ *
+ * Return 0 on success, -1 on error, and set errno.
+ *
+ */
+
+#include <string.h>
+#include <errno.h>
+#include "syslxint.h"
+
+unsigned char syslinux_adv[2*ADV_SIZE];
+
+#define ADV_MAGIC1 0x5a2d2fa5 /* Head signature */
+#define ADV_MAGIC2 0xa3041767 /* Total checksum */
+#define ADV_MAGIC3 0xdd28bf64 /* Tail signature */
+
+static void cleanup_adv(unsigned char *advbuf)
+{
+ int i;
+ uint32_t csum;
+
+ /* Make sure both copies agree, and update the checksum */
+ set_32(advbuf, ADV_MAGIC1);
+
+ csum = ADV_MAGIC2;
+ for (i = 8; i < ADV_SIZE-4; i += 4)
+ csum -= get_32(advbuf+i);
+
+ set_32(advbuf+4, csum);
+ set_32(advbuf+ADV_SIZE-4, ADV_MAGIC3);
+
+ memcpy(advbuf+ADV_SIZE, advbuf, ADV_SIZE);
+}
+
+int syslinux_setadv(int tag, size_t size, const void *data)
+{
+ uint8_t *p;
+ size_t left;
+ uint8_t advtmp[ADV_SIZE];
+
+ if ((unsigned)tag-1 > 254) {
+ errno = EINVAL;
+ return -1; /* Impossible tag value */
+ }
+
+ if (size > 255) {
+ errno = ENOSPC; /* Max 255 bytes for a data item */
+ return -1;
+ }
+
+ left = ADV_LEN;
+ p = advtmp;
+ memcpy(p, syslinux_adv+2*4, left); /* Make working copy */
+
+ while (left >= 2) {
+ uint8_t ptag = p[0];
+ size_t plen = p[1]+2;
+
+ if (ptag == ADV_END)
+ break;
+
+ if (ptag == tag) {
+ /* Found our tag. Delete it. */
+
+ if (plen >= left) {
+ /* Entire remainder is our tag */
+ break;
+ }
+ memmove(p, p+plen, left-plen);
+ } else {
+ /* Not our tag */
+ if (plen > left)
+ break; /* Corrupt tag (overrun) - overwrite it */
+
+ left -= plen;
+ p += plen;
+ }
+ }
+
+ /* Now (p, left) reflects the position to write in and how much space
+ we have for our data. */
+
+ if (size) {
+ if (left < size+2) {
+ errno = ENOSPC; /* Not enough space for data */
+ return -1;
+ }
+
+ *p++ = tag;
+ *p++ = size;
+ memcpy(p, data, size);
+ p += size;
+ left -= size+2;
+ }
+
+ memset(p, 0, left);
+
+ /* If we got here, everything went OK, commit the write */
+ memcpy(syslinux_adv+2*4, advtmp, ADV_LEN);
+ cleanup_adv(syslinux_adv);
+
+ return 0;
+}
+
+void syslinux_reset_adv(unsigned char *advbuf)
+{
+ /* Create an all-zero ADV */
+ memset(advbuf+2*4, 0, ADV_LEN);
+ cleanup_adv(advbuf);
+}
+
+static int adv_consistent(const unsigned char *p)
+{
+ int i;
+ uint32_t csum;
+
+ if (get_32(p) != ADV_MAGIC1 || get_32(p+ADV_SIZE-4) != ADV_MAGIC3)
+ return 0;
+
+ csum = 0;
+ for (i = 4; i < ADV_SIZE-4; i += 4)
+ csum += get_32(p+i);
+
+ return csum == ADV_MAGIC2;
+}
+
+/*
+ * Verify that an in-memory ADV is consistent, making the copies consistent.
+ * If neither copy is OK, return -1 and call syslinux_reset_adv().
+ */
+int syslinux_validate_adv(unsigned char *advbuf)
+{
+ if (adv_consistent(advbuf+0*ADV_SIZE)) {
+ memcpy(advbuf+ADV_SIZE, advbuf, ADV_SIZE);
+ return 0;
+ } else if (adv_consistent(advbuf+1*ADV_SIZE)) {
+ memcpy(advbuf, advbuf+ADV_SIZE, ADV_SIZE);
+ return 0;
+ } else {
+ syslinux_reset_adv(advbuf);
+ return -1;
+ }
+}
+
--- /dev/null
- * Copyright 2007 H. Peter Anvin - All Rights Reserved
+/* ----------------------------------------------------------------------- *
+ *
++ * Copyright 2007-2008 H. Peter Anvin - All Rights Reserved
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, Inc., 53 Temple Place Ste 330,
+ * Boston MA 02111-1307, USA; either version 2 of the License, or
+ * (at your option) any later version; incorporated herein by reference.
+ *
+ * ----------------------------------------------------------------------- */
+
+#ifndef SYSLXINT_H
+#define SYSLXINT_H
+
+#include "syslinux.h"
+
+/*
+ * Access functions for littleendian numbers, possibly misaligned.
+ */
+static inline uint8_t get_8(const unsigned char *p)
+{
+ return *(const uint8_t *)p;
+}
+
+static inline uint16_t get_16(const unsigned char *p)
+{
+#if defined(__i386__) || defined(__x86_64__)
+ /* Littleendian and unaligned-capable */
+ return *(const uint16_t *)p;
+#else
+ return (uint16_t)p[0] + ((uint16_t)p[1] << 8);
+#endif
+}
+
+static inline uint32_t get_32(const unsigned char *p)
+{
+#if defined(__i386__) || defined(__x86_64__)
+ /* Littleendian and unaligned-capable */
+ return *(const uint32_t *)p;
+#else
+ return (uint32_t)p[0] + ((uint32_t)p[1] << 8) +
+ ((uint32_t)p[2] << 16) + ((uint32_t)p[3] << 24);
+#endif
+}
+
+static inline void set_16(unsigned char *p, uint16_t v)
+{
+#if defined(__i386__) || defined(__x86_64__)
+ /* Littleendian and unaligned-capable */
+ *(uint16_t *)p = v;
+#else
+ p[0] = (v & 0xff);
+ p[1] = ((v >> 8) & 0xff);
+#endif
+}
+
+static inline void set_32(unsigned char *p, uint32_t v)
+{
+#if defined(__i386__) || defined(__x86_64__)
+ /* Littleendian and unaligned-capable */
+ *(uint32_t *)p = v;
+#else
+ p[0] = (v & 0xff);
+ p[1] = ((v >> 8) & 0xff);
+ p[2] = ((v >> 16) & 0xff);
+ p[3] = ((v >> 24) & 0xff);
+#endif
+}
+
+#define SECTOR_SHIFT 9 /* 512-byte sectors */
+#define SECTOR_SIZE (1 << SECTOR_SHIFT)
+
+#endif /* SYSLXINT_H */