OLD | NEW |
1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
2 * Use of this source code is governed by a BSD-style license that can be | 2 * Use of this source code is governed by a BSD-style license that can be |
3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
4 * | 4 * |
5 * Exports the kernel commandline from a given partition/image. | 5 * Exports the kernel commandline from a given partition/image. |
6 */ | 6 */ |
7 | 7 |
| 8 #include <getopt.h> |
8 #include <inttypes.h> /* For uint64_t */ | 9 #include <inttypes.h> /* For uint64_t */ |
9 #include <stdio.h> | 10 #include <stdio.h> |
10 #include <stdlib.h> | 11 #include <stdlib.h> |
11 #include <sys/mman.h> | 12 #include <sys/mman.h> |
12 #include <unistd.h> | 13 #include <unistd.h> |
13 | 14 |
14 #include "kernel_blob.h" | 15 #include "kernel_blob.h" |
15 #include "utility.h" | 16 #include "utility.h" |
16 #include "vboot_common.h" | 17 #include "vboot_common.h" |
17 #include "vboot_struct.h" | 18 #include "vboot_struct.h" |
18 | 19 |
| 20 enum { |
| 21 OPT_KLOADADDR = 1000, |
| 22 }; |
| 23 |
| 24 static struct option long_opts[] = { |
| 25 { "kloadaddr", 1, 0, OPT_KLOADADDR }, |
| 26 { NULL, 0, 0, 0 } |
| 27 }; |
| 28 |
19 /* Print help and return error */ | 29 /* Print help and return error */ |
20 static int PrintHelp(void) { | 30 static int PrintHelp(void) { |
21 puts("dump_kernel_config - Prints the kernel command line\n" | 31 puts("dump_kernel_config - Prints the kernel command line\n" |
22 "\n" | 32 "\n" |
23 "Usage: dump_kernel_config <image/blockdevice>\n" | 33 "Usage: dump_kernel_config [--kloadaddr <ADDRESS>] " |
| 34 "<image/blockdevice>\n" |
24 "\n" | 35 "\n" |
25 ""); | 36 ""); |
26 return 1; | 37 return 1; |
27 } | 38 } |
28 | 39 |
29 static uint8_t* find_kernel_config(uint8_t* blob, uint64_t blob_size) { | 40 static uint8_t* find_kernel_config(uint8_t* blob, uint64_t blob_size, |
| 41 uint64_t kernel_body_load_address) { |
30 VbKeyBlockHeader* key_block; | 42 VbKeyBlockHeader* key_block; |
31 VbKernelPreambleHeader* preamble; | 43 VbKernelPreambleHeader* preamble; |
32 struct linux_kernel_params *params; | 44 struct linux_kernel_params *params; |
33 uint32_t now = 0; | 45 uint32_t now = 0; |
34 uint32_t offset = 0; | 46 uint32_t offset = 0; |
35 | 47 |
36 /* Skip the key block */ | 48 /* Skip the key block */ |
37 key_block = (VbKeyBlockHeader*)blob; | 49 key_block = (VbKeyBlockHeader*)blob; |
38 now += key_block->key_block_size; | 50 now += key_block->key_block_size; |
39 if (now + blob > blob + blob_size) { | 51 if (now + blob > blob + blob_size) { |
40 error("key_block_size advances past the end of the blob\n"); | 52 error("key_block_size advances past the end of the blob\n"); |
41 return NULL; | 53 return NULL; |
42 } | 54 } |
43 | 55 |
44 /* Open up the preamble */ | 56 /* Open up the preamble */ |
45 preamble = (VbKernelPreambleHeader*)(blob + now); | 57 preamble = (VbKernelPreambleHeader*)(blob + now); |
46 now += preamble->preamble_size; | 58 now += preamble->preamble_size; |
47 if (now + blob > blob + blob_size) { | 59 if (now + blob > blob + blob_size) { |
48 error("preamble_size advances past the end of the blob\n"); | 60 error("preamble_size advances past the end of the blob\n"); |
49 return NULL; | 61 return NULL; |
50 } | 62 } |
51 | 63 |
52 /* The parameters are packed before the bootloader and there is no specific | 64 /* The parameters are packed before the bootloader and there is no specific |
53 * pointer to it so we just walk back by its allocated size. */ | 65 * pointer to it so we just walk back by its allocated size. */ |
54 offset = preamble->bootloader_address - | 66 offset = preamble->bootloader_address - |
55 (CROS_32BIT_ENTRY_ADDR + CROS_PARAMS_SIZE) + now; | 67 (kernel_body_load_address + CROS_PARAMS_SIZE) + now; |
56 if (offset > blob_size) { | 68 if (offset > blob_size) { |
57 error("params are outside of the memory blob: %x\n", offset); | 69 error("params are outside of the memory blob: %x\n", offset); |
58 return NULL; | 70 return NULL; |
59 } | 71 } |
60 params = (struct linux_kernel_params *)(blob + offset); | 72 params = (struct linux_kernel_params *)(blob + offset); |
61 | 73 |
62 /* Grab the offset to the kernel command line using the supplied pointer. */ | 74 /* Grab the offset to the kernel command line using the supplied pointer. */ |
63 offset = params->cmd_line_ptr - CROS_32BIT_ENTRY_ADDR + now; | 75 offset = params->cmd_line_ptr - kernel_body_load_address + now; |
64 if (offset > blob_size) { | 76 if (offset > blob_size) { |
65 error("cmdline is outside of the memory blob: %x\n", offset); | 77 error("cmdline is outside of the memory blob: %x\n", offset); |
66 return NULL; | 78 return NULL; |
67 } | 79 } |
68 return (uint8_t *)(blob + offset); | 80 return (uint8_t *)(blob + offset); |
69 } | 81 } |
70 | 82 |
71 static void* MapFile(const char *filename, size_t *size) { | 83 static void* MapFile(const char *filename, size_t *size) { |
72 FILE* f; | 84 FILE* f; |
73 uint8_t* buf; | 85 uint8_t* buf; |
(...skipping 23 matching lines...) Expand all Loading... |
97 return NULL; | 109 return NULL; |
98 } | 110 } |
99 | 111 |
100 fclose(f); | 112 fclose(f); |
101 return buf; | 113 return buf; |
102 } | 114 } |
103 | 115 |
104 int main(int argc, char* argv[]) { | 116 int main(int argc, char* argv[]) { |
105 uint8_t* blob; | 117 uint8_t* blob; |
106 size_t blob_size; | 118 size_t blob_size; |
107 char* infile = argv[1]; | 119 char* infile = NULL; |
108 uint8_t *config = NULL; | 120 uint8_t *config = NULL; |
| 121 uint64_t kernel_body_load_address = CROS_32BIT_ENTRY_ADDR; |
| 122 int parse_error = 0; |
| 123 char *e; |
| 124 int i; |
109 | 125 |
110 if (argc < 2) | 126 while (((i = getopt_long(argc, argv, ":", long_opts, NULL)) != -1) && |
| 127 !parse_error) { |
| 128 switch (i) { |
| 129 default: |
| 130 case '?': |
| 131 /* Unhandled option */ |
| 132 parse_error = 1; |
| 133 break; |
| 134 |
| 135 case 0: |
| 136 /* silently handled option */ |
| 137 break; |
| 138 |
| 139 case OPT_KLOADADDR: |
| 140 kernel_body_load_address = strtoul(optarg, &e, 0); |
| 141 if (!*optarg || (e && *e)) { |
| 142 fprintf(stderr, "Invalid --kloadaddr\n"); |
| 143 parse_error = 1; |
| 144 } |
| 145 break; |
| 146 } |
| 147 } |
| 148 |
| 149 if (optind >= argc) { |
| 150 fprintf(stderr, "Expected argument after options\n"); |
| 151 parse_error = 1; |
| 152 } else |
| 153 infile = argv[optind]; |
| 154 |
| 155 if (parse_error) |
111 return PrintHelp(); | 156 return PrintHelp(); |
112 | 157 |
113 if (!infile || !*infile) { | 158 if (!infile || !*infile) { |
114 error("Must specify filename\n"); | 159 error("Must specify filename\n"); |
115 return 1; | 160 return 1; |
116 } | 161 } |
117 | 162 |
118 /* Map the kernel image blob. */ | 163 /* Map the kernel image blob. */ |
119 blob = MapFile(infile, &blob_size); | 164 blob = MapFile(infile, &blob_size); |
120 if (!blob) { | 165 if (!blob) { |
121 error("Error reading input file\n"); | 166 error("Error reading input file\n"); |
122 return 1; | 167 return 1; |
123 } | 168 } |
124 | 169 |
125 config = find_kernel_config(blob, (uint64_t)blob_size); | 170 config = find_kernel_config(blob, (uint64_t)blob_size, |
| 171 kernel_body_load_address); |
126 if (!config) { | 172 if (!config) { |
127 error("Error parsing input file\n"); | 173 error("Error parsing input file\n"); |
128 munmap(blob, blob_size); | 174 munmap(blob, blob_size); |
129 return 1; | 175 return 1; |
130 } | 176 } |
131 | 177 |
132 printf("%.*s", CROS_CONFIG_SIZE, config); | 178 printf("%.*s", CROS_CONFIG_SIZE, config); |
133 munmap(blob, blob_size); | 179 munmap(blob, blob_size); |
134 return 0; | 180 return 0; |
135 } | 181 } |
OLD | NEW |