Files
OpenCellular/utility/dump_kernel_config.c
Che-Liang Chiou 2c0711bf54 Revert "Revert "Add --kloadaddr option to utilities""
This reverts commit bc7a84d9a1.

It was a false alarm that --kloadaddr causes chromeos-install on a
x86 targets to fail. The error of chromeos-install cannot be
reproduced, and judging by the reported error message, the error
should not be attributed to --kloadaddr, which has no effect in x86
targets. So --kloadaddr is restored.

Verification process are below:

(Verify that --kloadaddr option is restored)
$ dump_kernel_config -h
Expected argument after options
dump_kernel_config - Prints the kernel command line

Usage:  dump_kernel_config [--kloadaddr <ADDRESS>] <image/blockdevice>

(Setup a x86 target with kernel-next profile)
$ rm -rf /build/${X86_TARGET}
$ ./setup_board --board=${X86_TARGET} --profile=kernel-next
$ ./build_packages --board=${X86_TARGET}
$ ./build_image --board=${X86_TARGET}

(Run chromeos-install on target machine successfully)
$ /usr/sbin/chromeos-install

(Change directory to where image sits)
$ cd ~/trunk/src/build/images/${X86_TARGET}/latest

(Unpack Chromium OS image)
$ ./unpack_partitions.sh chromiumos_image.bin

(Verify that dump_kernel_config runs successfully)
$ dump_kernel_config part_2
console=tty2 init=/sbin/init add_efi_memmap boot=local noresume noswap
i915.modeset=1 cros_secure kern_guid=%U tpm_tis.force=1
tpm_tis.interrupts=0 nmi_watchdog=panic,lapic i8042.nomux=1
root=/dev/dm-0 quiet loglevel=1 rootwait ro dm_verity.error_behavior=3
dm_verity.max_bios=-1 dm_verity.dev_wait=1 dm="vroot none ro,0 1740800
verity %U+1 %U+1 1740800 1 sha1
c357e07395150770ce25ebc0e3c6d15941675c58"

(Run load_kernel_test)
$ load_kernel_test -b 2 chromiumos_image.bin
/usr/share/vboot/devkeys/recovery_key.vbpubk
Read 2088 bytes of key from /usr/share/vboot/devkeys/recovery_key.vbpubk
bootflags = 6
Reading from image: chromiumos_image.bin
Ending LBA: 3989538
Read(1, 1)
Read(2, 32)
Read(3989506, 32)
Read(3989538, 1)
Read(4096, 128)
Read(4224, 6472)
LoadKernel() returned 0
Partition number:   2
Bootloader address: 4345856
Bootloader size:    16384
Partition guid:     b2a453b0-a64a-5c4d-a957-1388cea384a5

R=marcheu@chromium.org,sjg@chromium.org
BUG=none
TEST=see verification process above

Review URL: http://codereview.chromium.org/6685079

Change-Id: I932753197550b853495f2c03e8880ad71df765a7
2011-03-22 13:15:19 +08:00

182 lines
4.3 KiB
C

/* Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*
* Exports the kernel commandline from a given partition/image.
*/
#include <getopt.h>
#include <inttypes.h> /* For uint64_t */
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <unistd.h>
#include "kernel_blob.h"
#include "utility.h"
#include "vboot_common.h"
#include "vboot_struct.h"
enum {
OPT_KLOADADDR = 1000,
};
static struct option long_opts[] = {
{ "kloadaddr", 1, 0, OPT_KLOADADDR },
{ NULL, 0, 0, 0 }
};
/* Print help and return error */
static int PrintHelp(void) {
puts("dump_kernel_config - Prints the kernel command line\n"
"\n"
"Usage: dump_kernel_config [--kloadaddr <ADDRESS>] "
"<image/blockdevice>\n"
"\n"
"");
return 1;
}
static uint8_t* find_kernel_config(uint8_t* blob, uint64_t blob_size,
uint64_t kernel_body_load_address) {
VbKeyBlockHeader* key_block;
VbKernelPreambleHeader* preamble;
struct linux_kernel_params *params;
uint32_t now = 0;
uint32_t offset = 0;
/* Skip the key block */
key_block = (VbKeyBlockHeader*)blob;
now += key_block->key_block_size;
if (now + blob > blob + blob_size) {
error("key_block_size advances past the end of the blob\n");
return NULL;
}
/* Open up the preamble */
preamble = (VbKernelPreambleHeader*)(blob + now);
now += preamble->preamble_size;
if (now + blob > blob + blob_size) {
error("preamble_size advances past the end of the blob\n");
return NULL;
}
/* The parameters are packed before the bootloader and there is no specific
* pointer to it so we just walk back by its allocated size. */
offset = preamble->bootloader_address -
(kernel_body_load_address + CROS_PARAMS_SIZE) + now;
if (offset > blob_size) {
error("params are outside of the memory blob: %x\n", offset);
return NULL;
}
params = (struct linux_kernel_params *)(blob + offset);
/* Grab the offset to the kernel command line using the supplied pointer. */
offset = params->cmd_line_ptr - kernel_body_load_address + now;
if (offset > blob_size) {
error("cmdline is outside of the memory blob: %x\n", offset);
return NULL;
}
return (uint8_t *)(blob + offset);
}
static void* MapFile(const char *filename, size_t *size) {
FILE* f;
uint8_t* buf;
long file_size = 0;
f = fopen(filename, "rb");
if (!f) {
VBDEBUG(("Unable to open file %s\n", filename));
return NULL;
}
fseek(f, 0, SEEK_END);
file_size = ftell(f);
rewind(f);
if (file_size <= 0) {
fclose(f);
return NULL;
}
*size = (size_t) file_size;
/* Uses a host primitive as this is not meant for firmware use. */
buf = mmap(NULL, *size, PROT_READ, MAP_PRIVATE, fileno(f), 0);
if (buf == MAP_FAILED) {
error("Failed to mmap the file %s\n", filename);
fclose(f);
return NULL;
}
fclose(f);
return buf;
}
int main(int argc, char* argv[]) {
uint8_t* blob;
size_t blob_size;
char* infile = NULL;
uint8_t *config = NULL;
uint64_t kernel_body_load_address = CROS_32BIT_ENTRY_ADDR;
int parse_error = 0;
char *e;
int i;
while (((i = getopt_long(argc, argv, ":", long_opts, NULL)) != -1) &&
!parse_error) {
switch (i) {
default:
case '?':
/* Unhandled option */
parse_error = 1;
break;
case 0:
/* silently handled option */
break;
case OPT_KLOADADDR:
kernel_body_load_address = strtoul(optarg, &e, 0);
if (!*optarg || (e && *e)) {
fprintf(stderr, "Invalid --kloadaddr\n");
parse_error = 1;
}
break;
}
}
if (optind >= argc) {
fprintf(stderr, "Expected argument after options\n");
parse_error = 1;
} else
infile = argv[optind];
if (parse_error)
return PrintHelp();
if (!infile || !*infile) {
error("Must specify filename\n");
return 1;
}
/* Map the kernel image blob. */
blob = MapFile(infile, &blob_size);
if (!blob) {
error("Error reading input file\n");
return 1;
}
config = find_kernel_config(blob, (uint64_t)blob_size,
kernel_body_load_address);
if (!config) {
error("Error parsing input file\n");
munmap(blob, blob_size);
return 1;
}
printf("%.*s", CROS_CONFIG_SIZE, config);
munmap(blob, blob_size);
return 0;
}