| OLD | NEW |
| (Empty) | |
| 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 |
| 3 * found in the LICENSE file. |
| 4 * |
| 5 * Exports the kernel commandline from a given partition/image. |
| 6 */ |
| 7 |
| 8 #include <inttypes.h> /* For uint64_t */ |
| 9 #include <stdio.h> |
| 10 #include <stdlib.h> |
| 11 #include <sys/mman.h> |
| 12 #include <unistd.h> |
| 13 |
| 14 #include "kernel_blob.h" |
| 15 #include "utility.h" |
| 16 #include "vboot_common.h" |
| 17 #include "vboot_struct.h" |
| 18 |
| 19 /* Print help and return error */ |
| 20 static int PrintHelp(void) { |
| 21 puts("dump_kernel_config - Prints the kernel command line\n" |
| 22 "\n" |
| 23 "Usage: dump_kernel_config <image/blockdevice>\n" |
| 24 "\n" |
| 25 ""); |
| 26 return 1; |
| 27 } |
| 28 |
| 29 static uint8_t* find_kernel_config(uint8_t* blob, uint64_t blob_size) { |
| 30 VbKeyBlockHeader* key_block; |
| 31 VbKernelPreambleHeader* preamble; |
| 32 struct linux_kernel_params *params; |
| 33 uint32_t now = 0; |
| 34 uint32_t offset = 0; |
| 35 |
| 36 /* Skip the key block */ |
| 37 key_block = (VbKeyBlockHeader*)blob; |
| 38 now += key_block->key_block_size; |
| 39 if (now + blob > blob + blob_size) { |
| 40 error("key_block_size advances past the end of the blob\n"); |
| 41 return NULL; |
| 42 } |
| 43 |
| 44 /* Open up the preamble */ |
| 45 preamble = (VbKernelPreambleHeader*)(blob + now); |
| 46 now += preamble->preamble_size; |
| 47 if (now + blob > blob + blob_size) { |
| 48 error("preamble_size advances past the end of the blob\n"); |
| 49 return NULL; |
| 50 } |
| 51 |
| 52 /* 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. */ |
| 54 offset = preamble->bootloader_address - |
| 55 (CROS_32BIT_ENTRY_ADDR + CROS_PARAMS_SIZE) + now; |
| 56 if (offset > blob_size) { |
| 57 error("params are outside of the memory blob: %x\n", offset); |
| 58 return NULL; |
| 59 } |
| 60 params = (struct linux_kernel_params *)(blob + offset); |
| 61 |
| 62 /* Grab the offset to the kernel command line using the supplied pointer. */ |
| 63 offset = params->cmd_line_ptr - CROS_32BIT_ENTRY_ADDR + now; |
| 64 if (offset > blob_size) { |
| 65 error("cmdline is outside of the memory blob: %x\n", offset); |
| 66 return NULL; |
| 67 } |
| 68 return (uint8_t *)(blob + offset); |
| 69 } |
| 70 |
| 71 static void* MapFile(const char *filename, size_t *size) { |
| 72 FILE* f; |
| 73 uint8_t* buf; |
| 74 long file_size = 0; |
| 75 |
| 76 f = fopen(filename, "rb"); |
| 77 if (!f) { |
| 78 debug("Unable to open file %s\n", filename); |
| 79 return NULL; |
| 80 } |
| 81 |
| 82 fseek(f, 0, SEEK_END); |
| 83 file_size = ftell(f); |
| 84 rewind(f); |
| 85 |
| 86 if (file_size <= 0) { |
| 87 fclose(f); |
| 88 return NULL; |
| 89 } |
| 90 *size = (size_t) file_size; |
| 91 |
| 92 /* Uses a host primitive as this is not meant for firmware use. */ |
| 93 buf = mmap(NULL, *size, PROT_READ, MAP_PRIVATE, fileno(f), 0); |
| 94 if (buf == MAP_FAILED) { |
| 95 error("Failed to mmap the file %s\n", filename); |
| 96 fclose(f); |
| 97 return NULL; |
| 98 } |
| 99 |
| 100 fclose(f); |
| 101 return buf; |
| 102 } |
| 103 |
| 104 int main(int argc, char* argv[]) { |
| 105 uint8_t* blob; |
| 106 size_t blob_size; |
| 107 char* infile = argv[1]; |
| 108 uint8_t *config = NULL; |
| 109 |
| 110 if (argc < 2) |
| 111 return PrintHelp(); |
| 112 |
| 113 if (!infile || !*infile) { |
| 114 error("Must specify filename\n"); |
| 115 return 1; |
| 116 } |
| 117 |
| 118 /* Map the kernel image blob. */ |
| 119 blob = MapFile(infile, &blob_size); |
| 120 if (!blob) { |
| 121 error("Error reading input file\n"); |
| 122 return 1; |
| 123 } |
| 124 |
| 125 config = find_kernel_config(blob, (uint64_t)blob_size); |
| 126 if (!config) { |
| 127 error("Error parsing input file\n"); |
| 128 munmap(blob, blob_size); |
| 129 return 1; |
| 130 } |
| 131 |
| 132 printf("%.*s", CROS_CONFIG_SIZE, config); |
| 133 munmap(blob, blob_size); |
| 134 return 0; |
| 135 } |
| OLD | NEW |