1616
1717#define pr_err (fmt , ...) fprintf(stderr, fmt, ##__VA_ARGS__)
1818
19+ /* Bootconfig footer is [size][csum][BOOTCONFIG_MAGIC]. */
20+ #define BOOTCONFIG_FOOTER_SIZE \
21+ (sizeof(uint32_t) * 2 + BOOTCONFIG_MAGIC_LEN)
22+
1923static int xbc_show_value (struct xbc_node * node , bool semicolon )
2024{
2125 const char * val , * eol ;
@@ -185,7 +189,7 @@ static int load_xbc_from_initrd(int fd, char **buf)
185189 if (ret < 0 )
186190 return - errno ;
187191
188- if (stat .st_size < 8 + BOOTCONFIG_MAGIC_LEN )
192+ if (stat .st_size < BOOTCONFIG_FOOTER_SIZE )
189193 return 0 ;
190194
191195 if (lseek (fd , - BOOTCONFIG_MAGIC_LEN , SEEK_END ) < 0 )
@@ -198,7 +202,7 @@ static int load_xbc_from_initrd(int fd, char **buf)
198202 if (memcmp (magic , BOOTCONFIG_MAGIC , BOOTCONFIG_MAGIC_LEN ) != 0 )
199203 return 0 ;
200204
201- if (lseek (fd , - ( 8 + BOOTCONFIG_MAGIC_LEN ) , SEEK_END ) < 0 )
205+ if (lseek (fd , - BOOTCONFIG_FOOTER_SIZE , SEEK_END ) < 0 )
202206 return pr_errno ("Failed to lseek for size" , - errno );
203207
204208 if (read (fd , & size , sizeof (uint32_t )) < 0 )
@@ -210,12 +214,12 @@ static int load_xbc_from_initrd(int fd, char **buf)
210214 csum = le32toh (csum );
211215
212216 /* Wrong size error */
213- if (stat .st_size < size + 8 + BOOTCONFIG_MAGIC_LEN ) {
217+ if (stat .st_size < size + BOOTCONFIG_FOOTER_SIZE ) {
214218 pr_err ("bootconfig size is too big\n" );
215219 return - E2BIG ;
216220 }
217221
218- if (lseek (fd , stat .st_size - (size + 8 + BOOTCONFIG_MAGIC_LEN ),
222+ if (lseek (fd , stat .st_size - (size + BOOTCONFIG_FOOTER_SIZE ),
219223 SEEK_SET ) < 0 )
220224 return pr_errno ("Failed to lseek" , - errno );
221225
@@ -346,7 +350,7 @@ static int delete_xbc(const char *path)
346350 ret = fstat (fd , & stat );
347351 if (!ret )
348352 ret = ftruncate (fd , stat .st_size
349- - size - 8 - BOOTCONFIG_MAGIC_LEN );
353+ - size - BOOTCONFIG_FOOTER_SIZE );
350354 if (ret )
351355 ret = - errno ;
352356 } /* Ignore if there is no boot config in initrd */
@@ -376,8 +380,7 @@ static int apply_xbc(const char *path, const char *xbc_path)
376380 csum = xbc_calc_checksum (buf , size );
377381
378382 /* 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 );
381384 if (!data )
382385 return - ENOMEM ;
383386 memcpy (data , buf , size );
@@ -425,7 +428,7 @@ static int apply_xbc(const char *path, const char *xbc_path)
425428 }
426429
427430 /* 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 ;
429432 pad = ((total_size + BOOTCONFIG_ALIGN - 1 ) & (~BOOTCONFIG_ALIGN_MASK )) - total_size ;
430433 size += pad ;
431434
0 commit comments