xref: /aosp_15_r20/external/vboot_reference/futility/cmd_dump_kernel_config.c (revision 8617a60d3594060b7ecbd21bc622a7c14f3cf2bc)
1 /* Copyright 2012 The ChromiumOS Authors
2  * Use of this source code is governed by a BSD-style license that can be
3  * found in the LICENSE file.
4  *
5  * Exports the kernel commandline from a given partition/image.
6  */
7 
8 #include <getopt.h>
9 #include <stdio.h>
10 #include <sys/mman.h>
11 #include <unistd.h>
12 
13 #include "futility.h"
14 #include "vboot_host.h"
15 
16 enum {
17 	OPT_KLOADADDR = 1000,
18 	OPT_HELP,
19 };
20 
21 static const struct option long_opts[] = {
22 	{"kloadaddr", 1, NULL, OPT_KLOADADDR},
23 	{"help", 0, 0, OPT_HELP},
24 	{NULL, 0, NULL, 0}
25 };
26 
27 /* Print help and return error */
print_help(int argc,char * argv[])28 static void print_help(int argc, char *argv[])
29 {
30 	printf("\nUsage:  " MYNAME " %s [--kloadaddr ADDRESS] "
31 	       "KERNEL_PARTITION\n\n", argv[0]);
32 }
33 
do_dump_kern_cfg(int argc,char * argv[])34 static int do_dump_kern_cfg(int argc, char *argv[])
35 {
36 	char *infile = NULL;
37 	char *config = NULL;
38 	uint64_t kernel_body_load_address = USE_PREAMBLE_LOAD_ADDR;
39 	int parse_error = 0;
40 	char *e;
41 	int i;
42 
43 	while (((i = getopt_long(argc, argv, ":", long_opts, NULL)) != -1) &&
44 	       !parse_error) {
45 		switch (i) {
46 		default:
47 		case '?':
48 			/* Unhandled option */
49 			parse_error = 1;
50 			break;
51 
52 		case 0:
53 			/* silently handled option */
54 			break;
55 
56 		case OPT_KLOADADDR:
57 			kernel_body_load_address = strtoul(optarg, &e, 0);
58 			if (!*optarg || (e && *e)) {
59 				ERROR("Invalid --kloadaddr\n");
60 				parse_error = 1;
61 			}
62 			break;
63 
64 		case OPT_HELP:
65 			print_help(argc, argv);
66 			return 0;
67 		}
68 	}
69 
70 	if (optind >= argc) {
71 		ERROR("Expected argument after options\n");
72 		parse_error = 1;
73 	} else
74 		infile = argv[optind];
75 
76 	if (parse_error) {
77 		print_help(argc, argv);
78 		return 1;
79 	}
80 
81 	if (!infile || !*infile) {
82 		ERROR("Must specify filename\n");
83 		return 1;
84 	}
85 
86 	config = FindKernelConfig(infile, kernel_body_load_address);
87 	if (!config)
88 		return 1;
89 
90 	printf("%s", config);
91 
92 	free(config);
93 	return 0;
94 }
95 
96 DECLARE_FUTIL_COMMAND(dump_kernel_config, do_dump_kern_cfg, VBOOT_VERSION_ALL,
97 		      "Prints the kernel command line");
98