1 /* Copyright 2012 The Chromium OS Authors. All rights reserved.
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 };
19 
20 static const struct option long_opts[] = {
21 	{"kloadaddr", 1, NULL, OPT_KLOADADDR},
22 	{NULL, 0, NULL, 0}
23 };
24 
25 /* Print help and return error */
PrintHelp(const char * progname)26 static void PrintHelp(const char *progname)
27 {
28 	printf("\nUsage:  " MYNAME " %s [--kloadaddr ADDRESS] "
29 	       "KERNEL_PARTITION\n\n", progname);
30 }
31 
do_dump_kernel_config(int argc,char * argv[])32 static int do_dump_kernel_config(int argc, char *argv[])
33 {
34 	char *infile = NULL;
35 	char *config = NULL;
36 	uint64_t kernel_body_load_address = USE_PREAMBLE_LOAD_ADDR;
37 	int parse_error = 0;
38 	char *e;
39 	int i;
40 
41 	while (((i = getopt_long(argc, argv, ":", long_opts, NULL)) != -1) &&
42 	       !parse_error) {
43 		switch (i) {
44 		default:
45 		case '?':
46 			/* Unhandled option */
47 			parse_error = 1;
48 			break;
49 
50 		case 0:
51 			/* silently handled option */
52 			break;
53 
54 		case OPT_KLOADADDR:
55 			kernel_body_load_address = strtoul(optarg, &e, 0);
56 			if (!*optarg || (e && *e)) {
57 				fprintf(stderr, "Invalid --kloadaddr\n");
58 				parse_error = 1;
59 			}
60 			break;
61 		}
62 	}
63 
64 	if (optind >= argc) {
65 		fprintf(stderr, "Expected argument after options\n");
66 		parse_error = 1;
67 	} else
68 		infile = argv[optind];
69 
70 	if (parse_error) {
71 		PrintHelp(argv[0]);
72 		return 1;
73 	}
74 
75 	if (!infile || !*infile) {
76 		fprintf(stderr, "Must specify filename\n");
77 		return 1;
78 	}
79 
80 	config = FindKernelConfig(infile, kernel_body_load_address);
81 	if (!config)
82 		return 1;
83 
84 	printf("%s", config);
85 
86 	free(config);
87 	return 0;
88 }
89 
90 DECLARE_FUTIL_COMMAND(dump_kernel_config, do_dump_kernel_config,
91 		      VBOOT_VERSION_ALL,
92 		      "Prints the kernel command line",
93 		      PrintHelp);
94