Move modify_adv() into common code
[profile/ivi/syslinux.git] / libinstaller / setadv.c
1 /* ----------------------------------------------------------------------- *
2  *
3  *   Copyright 2007-2008 H. Peter Anvin - All Rights Reserved
4  *   Copyright 2010 Intel Corporation; author: H. Peter Anvin
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
9  *   Boston MA 02111-1307, USA; either version 2 of the License, or
10  *   (at your option) any later version; incorporated herein by reference.
11  *
12  * ----------------------------------------------------------------------- */
13
14 /*
15  * setadv.c
16  *
17  * (Over)write a data item in the auxilliary data vector.  To
18  * delete an item, set its length to zero.
19  *
20  * Return 0 on success, -1 on error, and set errno.
21  *
22  */
23 #define  _GNU_SOURCE
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <stddef.h>
28 #include <stdint.h>
29 #include <string.h>
30 #include <errno.h>
31 #include "syslxint.h"
32 #include "syslxcom.h"
33
34 unsigned char syslinux_adv[2 * ADV_SIZE];
35
36 #define ADV_MAGIC1      0x5a2d2fa5      /* Head signature */
37 #define ADV_MAGIC2      0xa3041767      /* Total checksum */
38 #define ADV_MAGIC3      0xdd28bf64      /* Tail signature */
39
40 static void cleanup_adv(unsigned char *advbuf)
41 {
42     int i;
43     uint32_t csum;
44
45     /* Make sure both copies agree, and update the checksum */
46     set_32((uint32_t *) advbuf, ADV_MAGIC1);
47
48     csum = ADV_MAGIC2;
49     for (i = 8; i < ADV_SIZE - 4; i += 4)
50         csum -= get_32((uint32_t *) (advbuf + i));
51
52     set_32((uint32_t *) (advbuf + 4), csum);
53     set_32((uint32_t *) (advbuf + ADV_SIZE - 4), ADV_MAGIC3);
54
55     memcpy(advbuf + ADV_SIZE, advbuf, ADV_SIZE);
56 }
57
58 int syslinux_setadv(int tag, size_t size, const void *data)
59 {
60     uint8_t *p;
61     size_t left;
62     uint8_t advtmp[ADV_SIZE];
63
64     if ((unsigned)tag - 1 > 254) {
65         errno = EINVAL;
66         return -1;              /* Impossible tag value */
67     }
68
69     if (size > 255) {
70         errno = ENOSPC;         /* Max 255 bytes for a data item */
71         return -1;
72     }
73
74     left = ADV_LEN;
75     p = advtmp;
76     memcpy(p, syslinux_adv + 2 * 4, left);      /* Make working copy */
77
78     while (left >= 2) {
79         uint8_t ptag = p[0];
80         size_t plen = p[1] + 2;
81
82         if (ptag == ADV_END)
83             break;
84
85         if (ptag == tag) {
86             /* Found our tag.  Delete it. */
87
88             if (plen >= left) {
89                 /* Entire remainder is our tag */
90                 break;
91             }
92             memmove(p, p + plen, left - plen);
93         } else {
94             /* Not our tag */
95             if (plen > left)
96                 break;          /* Corrupt tag (overrun) - overwrite it */
97
98             left -= plen;
99             p += plen;
100         }
101     }
102
103     /* Now (p, left) reflects the position to write in and how much space
104        we have for our data. */
105
106     if (size) {
107         if (left < size + 2) {
108             errno = ENOSPC;     /* Not enough space for data */
109             return -1;
110         }
111
112         *p++ = tag;
113         *p++ = size;
114         memcpy(p, data, size);
115         p += size;
116         left -= size + 2;
117     }
118
119     memset(p, 0, left);
120
121     /* If we got here, everything went OK, commit the write */
122     memcpy(syslinux_adv + 2 * 4, advtmp, ADV_LEN);
123     cleanup_adv(syslinux_adv);
124
125     return 0;
126 }
127
128 void syslinux_reset_adv(unsigned char *advbuf)
129 {
130     /* Create an all-zero ADV */
131     memset(advbuf + 2 * 4, 0, ADV_LEN);
132     cleanup_adv(advbuf);
133 }
134
135 static int adv_consistent(const unsigned char *p)
136 {
137     int i;
138     uint32_t csum;
139
140     if (get_32((uint32_t *) p) != ADV_MAGIC1 ||
141         get_32((uint32_t *) (p + ADV_SIZE - 4)) != ADV_MAGIC3)
142         return 0;
143
144     csum = 0;
145     for (i = 4; i < ADV_SIZE - 4; i += 4)
146         csum += get_32((uint32_t *) (p + i));
147
148     return csum == ADV_MAGIC2;
149 }
150
151 /*
152  * Verify that an in-memory ADV is consistent, making the copies consistent.
153  * If neither copy is OK, return -1 and call syslinux_reset_adv().
154  */
155 int syslinux_validate_adv(unsigned char *advbuf)
156 {
157     if (adv_consistent(advbuf + 0 * ADV_SIZE)) {
158         memcpy(advbuf + ADV_SIZE, advbuf, ADV_SIZE);
159         return 0;
160     } else if (adv_consistent(advbuf + 1 * ADV_SIZE)) {
161         memcpy(advbuf, advbuf + ADV_SIZE, ADV_SIZE);
162         return 0;
163     } else {
164         syslinux_reset_adv(advbuf);
165         return -1;
166     }
167 }