16
16
17
17
#define pr_err (fmt , ...) fprintf(stderr, fmt, ##__VA_ARGS__)
18
18
19
+ /* Bootconfig footer is [size][csum][BOOTCONFIG_MAGIC]. */
20
+ #define BOOTCONFIG_FOOTER_SIZE \
21
+ (sizeof(uint32_t) * 2 + BOOTCONFIG_MAGIC_LEN)
22
+
19
23
static int xbc_show_value (struct xbc_node * node , bool semicolon )
20
24
{
21
25
const char * val , * eol ;
@@ -185,7 +189,7 @@ static int load_xbc_from_initrd(int fd, char **buf)
185
189
if (ret < 0 )
186
190
return - errno ;
187
191
188
- if (stat .st_size < 8 + BOOTCONFIG_MAGIC_LEN )
192
+ if (stat .st_size < BOOTCONFIG_FOOTER_SIZE )
189
193
return 0 ;
190
194
191
195
if (lseek (fd , - BOOTCONFIG_MAGIC_LEN , SEEK_END ) < 0 )
@@ -198,7 +202,7 @@ static int load_xbc_from_initrd(int fd, char **buf)
198
202
if (memcmp (magic , BOOTCONFIG_MAGIC , BOOTCONFIG_MAGIC_LEN ) != 0 )
199
203
return 0 ;
200
204
201
- if (lseek (fd , - ( 8 + BOOTCONFIG_MAGIC_LEN ) , SEEK_END ) < 0 )
205
+ if (lseek (fd , - BOOTCONFIG_FOOTER_SIZE , SEEK_END ) < 0 )
202
206
return pr_errno ("Failed to lseek for size" , - errno );
203
207
204
208
if (read (fd , & size , sizeof (uint32_t )) < 0 )
@@ -210,12 +214,12 @@ static int load_xbc_from_initrd(int fd, char **buf)
210
214
csum = le32toh (csum );
211
215
212
216
/* Wrong size error */
213
- if (stat .st_size < size + 8 + BOOTCONFIG_MAGIC_LEN ) {
217
+ if (stat .st_size < size + BOOTCONFIG_FOOTER_SIZE ) {
214
218
pr_err ("bootconfig size is too big\n" );
215
219
return - E2BIG ;
216
220
}
217
221
218
- if (lseek (fd , stat .st_size - (size + 8 + BOOTCONFIG_MAGIC_LEN ),
222
+ if (lseek (fd , stat .st_size - (size + BOOTCONFIG_FOOTER_SIZE ),
219
223
SEEK_SET ) < 0 )
220
224
return pr_errno ("Failed to lseek" , - errno );
221
225
@@ -346,7 +350,7 @@ static int delete_xbc(const char *path)
346
350
ret = fstat (fd , & stat );
347
351
if (!ret )
348
352
ret = ftruncate (fd , stat .st_size
349
- - size - 8 - BOOTCONFIG_MAGIC_LEN );
353
+ - size - BOOTCONFIG_FOOTER_SIZE );
350
354
if (ret )
351
355
ret = - errno ;
352
356
} /* Ignore if there is no boot config in initrd */
@@ -376,8 +380,7 @@ static int apply_xbc(const char *path, const char *xbc_path)
376
380
csum = xbc_calc_checksum (buf , size );
377
381
378
382
/* Backup the bootconfig data */
379
- data = calloc (size + BOOTCONFIG_ALIGN +
380
- sizeof (uint32_t ) + sizeof (uint32_t ) + BOOTCONFIG_MAGIC_LEN , 1 );
383
+ data = calloc (size + BOOTCONFIG_ALIGN + BOOTCONFIG_FOOTER_SIZE , 1 );
381
384
if (!data )
382
385
return - ENOMEM ;
383
386
memcpy (data , buf , size );
@@ -425,7 +428,7 @@ static int apply_xbc(const char *path, const char *xbc_path)
425
428
}
426
429
427
430
/* To align up the total size to BOOTCONFIG_ALIGN, get padding size */
428
- total_size = stat .st_size + size + sizeof ( uint32_t ) * 2 + BOOTCONFIG_MAGIC_LEN ;
431
+ total_size = stat .st_size + size + BOOTCONFIG_FOOTER_SIZE ;
429
432
pad = ((total_size + BOOTCONFIG_ALIGN - 1 ) & (~BOOTCONFIG_ALIGN_MASK )) - total_size ;
430
433
size += pad ;
431
434
0 commit comments