main.c (20c7775aecea04d8ca322039969d49dcf568e0e9) main.c (e86843580d1bb1ce12544bca3115cf11d51603ff)
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Boot config tool for initrd image
4 */
5#include <stdio.h>
6#include <stdlib.h>
7#include <sys/types.h>
8#include <sys/stat.h>
9#include <fcntl.h>
10#include <unistd.h>
11#include <string.h>
12#include <errno.h>
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Boot config tool for initrd image
4 */
5#include <stdio.h>
6#include <stdlib.h>
7#include <sys/types.h>
8#include <sys/stat.h>
9#include <fcntl.h>
10#include <unistd.h>
11#include <string.h>
12#include <errno.h>
13#include <endian.h>
13
14#include <linux/kernel.h>
15#include <linux/bootconfig.h>
16
17static int xbc_show_value(struct xbc_node *node, bool semicolon)
18{
19 const char *val, *eol;
20 char q;

--- 121 unchanged lines hidden (view full) ---

142
143 ret = load_xbc_fd(fd, buf, stat.st_size);
144
145 close(fd);
146
147 return ret;
148}
149
14
15#include <linux/kernel.h>
16#include <linux/bootconfig.h>
17
18static int xbc_show_value(struct xbc_node *node, bool semicolon)
19{
20 const char *val, *eol;
21 char q;

--- 121 unchanged lines hidden (view full) ---

143
144 ret = load_xbc_fd(fd, buf, stat.st_size);
145
146 close(fd);
147
148 return ret;
149}
150
151static int pr_errno(const char *msg, int err)
152{
153 pr_err("%s: %d\n", msg, err);
154 return err;
155}
156
150static int load_xbc_from_initrd(int fd, char **buf)
151{
152 struct stat stat;
153 int ret;
154 u32 size = 0, csum = 0, rcsum;
155 char magic[BOOTCONFIG_MAGIC_LEN];
156 const char *msg;
157
158 ret = fstat(fd, &stat);
159 if (ret < 0)
160 return -errno;
161
162 if (stat.st_size < 8 + BOOTCONFIG_MAGIC_LEN)
163 return 0;
164
157static int load_xbc_from_initrd(int fd, char **buf)
158{
159 struct stat stat;
160 int ret;
161 u32 size = 0, csum = 0, rcsum;
162 char magic[BOOTCONFIG_MAGIC_LEN];
163 const char *msg;
164
165 ret = fstat(fd, &stat);
166 if (ret < 0)
167 return -errno;
168
169 if (stat.st_size < 8 + BOOTCONFIG_MAGIC_LEN)
170 return 0;
171
165 if (lseek(fd, -BOOTCONFIG_MAGIC_LEN, SEEK_END) < 0) {
166 pr_err("Failed to lseek: %d\n", -errno);
167 return -errno;
168 }
172 if (lseek(fd, -BOOTCONFIG_MAGIC_LEN, SEEK_END) < 0)
173 return pr_errno("Failed to lseek for magic", -errno);
174
169 if (read(fd, magic, BOOTCONFIG_MAGIC_LEN) < 0)
175 if (read(fd, magic, BOOTCONFIG_MAGIC_LEN) < 0)
170 return -errno;
176 return pr_errno("Failed to read", -errno);
177
171 /* Check the bootconfig magic bytes */
172 if (memcmp(magic, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN) != 0)
173 return 0;
174
178 /* Check the bootconfig magic bytes */
179 if (memcmp(magic, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN) != 0)
180 return 0;
181
175 if (lseek(fd, -(8 + BOOTCONFIG_MAGIC_LEN), SEEK_END) < 0) {
176 pr_err("Failed to lseek: %d\n", -errno);
177 return -errno;
178 }
182 if (lseek(fd, -(8 + BOOTCONFIG_MAGIC_LEN), SEEK_END) < 0)
183 return pr_errno("Failed to lseek for size", -errno);
179
180 if (read(fd, &size, sizeof(u32)) < 0)
184
185 if (read(fd, &size, sizeof(u32)) < 0)
181 return -errno;
186 return pr_errno("Failed to read size", -errno);
187 size = le32toh(size);
182
183 if (read(fd, &csum, sizeof(u32)) < 0)
188
189 if (read(fd, &csum, sizeof(u32)) < 0)
184 return -errno;
190 return pr_errno("Failed to read checksum", -errno);
191 csum = le32toh(csum);
185
186 /* Wrong size error */
187 if (stat.st_size < size + 8 + BOOTCONFIG_MAGIC_LEN) {
188 pr_err("bootconfig size is too big\n");
189 return -E2BIG;
190 }
191
192 if (lseek(fd, stat.st_size - (size + 8 + BOOTCONFIG_MAGIC_LEN),
192
193 /* Wrong size error */
194 if (stat.st_size < size + 8 + BOOTCONFIG_MAGIC_LEN) {
195 pr_err("bootconfig size is too big\n");
196 return -E2BIG;
197 }
198
199 if (lseek(fd, stat.st_size - (size + 8 + BOOTCONFIG_MAGIC_LEN),
193 SEEK_SET) < 0) {
194 pr_err("Failed to lseek: %d\n", -errno);
195 return -errno;
196 }
200 SEEK_SET) < 0)
201 return pr_errno("Failed to lseek", -errno);
197
198 ret = load_xbc_fd(fd, buf, size);
199 if (ret < 0)
200 return ret;
201
202 /* Wrong Checksum */
203 rcsum = checksum((unsigned char *)*buf, size);
204 if (csum != rcsum) {

--- 52 unchanged lines hidden (view full) ---

257static int show_xbc(const char *path, bool list)
258{
259 int ret, fd;
260 char *buf = NULL;
261 struct stat st;
262
263 ret = stat(path, &st);
264 if (ret < 0) {
202
203 ret = load_xbc_fd(fd, buf, size);
204 if (ret < 0)
205 return ret;
206
207 /* Wrong Checksum */
208 rcsum = checksum((unsigned char *)*buf, size);
209 if (csum != rcsum) {

--- 52 unchanged lines hidden (view full) ---

262static int show_xbc(const char *path, bool list)
263{
264 int ret, fd;
265 char *buf = NULL;
266 struct stat st;
267
268 ret = stat(path, &st);
269 if (ret < 0) {
265 pr_err("Failed to stat %s: %d\n", path, -errno);
266 return -errno;
270 ret = -errno;
271 pr_err("Failed to stat %s: %d\n", path, ret);
272 return ret;
267 }
268
269 fd = open(path, O_RDONLY);
270 if (fd < 0) {
273 }
274
275 fd = open(path, O_RDONLY);
276 if (fd < 0) {
271 pr_err("Failed to open initrd %s: %d\n", path, fd);
272 return -errno;
277 ret = -errno;
278 pr_err("Failed to open initrd %s: %d\n", path, ret);
279 return ret;
273 }
274
275 ret = load_xbc_from_initrd(fd, &buf);
276 close(fd);
277 if (ret < 0) {
278 pr_err("Failed to load a boot config from initrd: %d\n", ret);
279 goto out;
280 }

--- 21 unchanged lines hidden (view full) ---

302static int delete_xbc(const char *path)
303{
304 struct stat stat;
305 int ret = 0, fd, size;
306 char *buf = NULL;
307
308 fd = open(path, O_RDWR);
309 if (fd < 0) {
280 }
281
282 ret = load_xbc_from_initrd(fd, &buf);
283 close(fd);
284 if (ret < 0) {
285 pr_err("Failed to load a boot config from initrd: %d\n", ret);
286 goto out;
287 }

--- 21 unchanged lines hidden (view full) ---

309static int delete_xbc(const char *path)
310{
311 struct stat stat;
312 int ret = 0, fd, size;
313 char *buf = NULL;
314
315 fd = open(path, O_RDWR);
316 if (fd < 0) {
310 pr_err("Failed to open initrd %s: %d\n", path, fd);
311 return -errno;
317 ret = -errno;
318 pr_err("Failed to open initrd %s: %d\n", path, ret);
319 return ret;
312 }
313
314 size = load_xbc_from_initrd(fd, &buf);
315 if (size < 0) {
316 ret = size;
317 pr_err("Failed to load a boot config from initrd: %d\n", ret);
318 } else if (size > 0) {
319 ret = fstat(fd, &stat);

--- 7 unchanged lines hidden (view full) ---

327 close(fd);
328 free(buf);
329
330 return ret;
331}
332
333static int apply_xbc(const char *path, const char *xbc_path)
334{
320 }
321
322 size = load_xbc_from_initrd(fd, &buf);
323 if (size < 0) {
324 ret = size;
325 pr_err("Failed to load a boot config from initrd: %d\n", ret);
326 } else if (size > 0) {
327 ret = fstat(fd, &stat);

--- 7 unchanged lines hidden (view full) ---

335 close(fd);
336 free(buf);
337
338 return ret;
339}
340
341static int apply_xbc(const char *path, const char *xbc_path)
342{
343 char *buf, *data, *p;
344 size_t total_size;
345 struct stat stat;
346 const char *msg;
335 u32 size, csum;
347 u32 size, csum;
336 char *buf, *data;
348 int pos, pad;
337 int ret, fd;
349 int ret, fd;
338 const char *msg;
339 int pos;
340
341 ret = load_xbc_file(xbc_path, &buf);
342 if (ret < 0) {
343 pr_err("Failed to load %s : %d\n", xbc_path, ret);
344 return ret;
345 }
346 size = strlen(buf) + 1;
347 csum = checksum((unsigned char *)buf, size);
348
350
351 ret = load_xbc_file(xbc_path, &buf);
352 if (ret < 0) {
353 pr_err("Failed to load %s : %d\n", xbc_path, ret);
354 return ret;
355 }
356 size = strlen(buf) + 1;
357 csum = checksum((unsigned char *)buf, size);
358
349 /* Prepare xbc_path data */
350 data = malloc(size + 8);
359 /* Backup the bootconfig data */
360 data = calloc(size + BOOTCONFIG_ALIGN +
361 sizeof(u32) + sizeof(u32) + BOOTCONFIG_MAGIC_LEN, 1);
351 if (!data)
352 return -ENOMEM;
362 if (!data)
363 return -ENOMEM;
353 strcpy(data, buf);
354 *(u32 *)(data + size) = size;
355 *(u32 *)(data + size + 4) = csum;
364 memcpy(data, buf, size);
356
357 /* Check the data format */
358 ret = xbc_init(buf, &msg, &pos);
359 if (ret < 0) {
360 show_xbc_error(data, msg, pos);
361 free(data);
362 free(buf);
363

--- 14 unchanged lines hidden (view full) ---

378 pr_err("Failed to delete previous boot config: %d\n", ret);
379 free(data);
380 return ret;
381 }
382
383 /* Apply new one */
384 fd = open(path, O_RDWR | O_APPEND);
385 if (fd < 0) {
365
366 /* Check the data format */
367 ret = xbc_init(buf, &msg, &pos);
368 if (ret < 0) {
369 show_xbc_error(data, msg, pos);
370 free(data);
371 free(buf);
372

--- 14 unchanged lines hidden (view full) ---

387 pr_err("Failed to delete previous boot config: %d\n", ret);
388 free(data);
389 return ret;
390 }
391
392 /* Apply new one */
393 fd = open(path, O_RDWR | O_APPEND);
394 if (fd < 0) {
386 pr_err("Failed to open %s: %d\n", path, fd);
395 ret = -errno;
396 pr_err("Failed to open %s: %d\n", path, ret);
387 free(data);
397 free(data);
388 return fd;
398 return ret;
389 }
390 /* TODO: Ensure the @path is initramfs/initrd image */
399 }
400 /* TODO: Ensure the @path is initramfs/initrd image */
391 ret = write(fd, data, size + 8);
392 if (ret < 0) {
393 pr_err("Failed to apply a boot config: %d\n", ret);
401 if (fstat(fd, &stat) < 0) {
402 pr_err("Failed to get the size of %s\n", path);
394 goto out;
395 }
403 goto out;
404 }
396 /* Write a magic word of the bootconfig */
397 ret = write(fd, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN);
398 if (ret < 0) {
399 pr_err("Failed to apply a boot config magic: %d\n", ret);
400 goto out;
401 }
402 ret = 0;
405
406 /* To align up the total size to BOOTCONFIG_ALIGN, get padding size */
407 total_size = stat.st_size + size + sizeof(u32) * 2 + BOOTCONFIG_MAGIC_LEN;
408 pad = ((total_size + BOOTCONFIG_ALIGN - 1) & (~BOOTCONFIG_ALIGN_MASK)) - total_size;
409 size += pad;
410
411 /* Add a footer */
412 p = data + size;
413 *(u32 *)p = htole32(size);
414 p += sizeof(u32);
415
416 *(u32 *)p = htole32(csum);
417 p += sizeof(u32);
418
419 memcpy(p, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN);
420 p += BOOTCONFIG_MAGIC_LEN;
421
422 total_size = p - data;
423
424 ret = write(fd, data, total_size);
425 if (ret < total_size) {
426 if (ret < 0)
427 ret = -errno;
428 pr_err("Failed to apply a boot config: %d\n", ret);
429 if (ret >= 0)
430 goto out_rollback;
431 } else
432 ret = 0;
433
403out:
404 close(fd);
405 free(data);
406
407 return ret;
434out:
435 close(fd);
436 free(data);
437
438 return ret;
439
440out_rollback:
441 /* Map the partial write to -ENOSPC */
442 if (ret >= 0)
443 ret = -ENOSPC;
444 if (ftruncate(fd, stat.st_size) < 0) {
445 ret = -errno;
446 pr_err("Failed to rollback the write error: %d\n", ret);
447 pr_err("The initrd %s may be corrupted. Recommend to rebuild.\n", path);
448 }
449 goto out;
408}
409
410static int usage(void)
411{
412 printf("Usage: bootconfig [OPTIONS] <INITRD>\n"
413 "Or bootconfig <CONFIG>\n"
414 " Apply, delete or show boot config to initrd.\n"
415 " Options:\n"

--- 50 unchanged lines hidden ---
450}
451
452static int usage(void)
453{
454 printf("Usage: bootconfig [OPTIONS] <INITRD>\n"
455 "Or bootconfig <CONFIG>\n"
456 " Apply, delete or show boot config to initrd.\n"
457 " Options:\n"

--- 50 unchanged lines hidden ---