Bill Richardson | 6f39615 | 2014-07-15 12:52:19 -0700 | [diff] [blame] | 1 | /* Copyright 2012 The Chromium OS Authors. All rights reserved. |
Randall Spangler | 17f8d34 | 2013-01-11 10:55:11 -0800 | [diff] [blame] | 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 <stdio.h> |
Bill Richardson | 28b65ca | 2013-03-28 17:31:24 -0700 | [diff] [blame] | 9 | #include <string.h> |
Randall Spangler | 17f8d34 | 2013-01-11 10:55:11 -0800 | [diff] [blame] | 10 | #include <sys/mman.h> |
Nam T. Nguyen | aa4ee34 | 2015-02-12 11:02:30 -0800 | [diff] [blame] | 11 | #include <sys/fcntl.h> |
| 12 | #include <sys/stat.h> |
| 13 | #include <sys/types.h> |
| 14 | #include <unistd.h> |
Randall Spangler | 17f8d34 | 2013-01-11 10:55:11 -0800 | [diff] [blame] | 15 | |
Randall Spangler | 17f8d34 | 2013-01-11 10:55:11 -0800 | [diff] [blame] | 16 | #include "host_common.h" |
| 17 | #include "kernel_blob.h" |
| 18 | #include "vboot_api.h" |
Bill Richardson | 0c3ba24 | 2013-03-29 11:09:30 -0700 | [diff] [blame] | 19 | #include "vboot_host.h" |
Randall Spangler | 17f8d34 | 2013-01-11 10:55:11 -0800 | [diff] [blame] | 20 | |
Nam T. Nguyen | f44ebbe | 2015-02-12 11:10:35 -0800 | [diff] [blame] | 21 | #ifdef USE_MTD |
| 22 | #include <linux/major.h> |
| 23 | #include <mtd/mtd-user.h> |
| 24 | #include <mtdutils.h> |
| 25 | #endif |
| 26 | |
Nam T. Nguyen | aa4ee34 | 2015-02-12 11:02:30 -0800 | [diff] [blame] | 27 | typedef ssize_t (*ReadFullyFn)(void *ctx, void *buf, size_t count); |
Randall Spangler | 17f8d34 | 2013-01-11 10:55:11 -0800 | [diff] [blame] | 28 | |
Nam T. Nguyen | aa4ee34 | 2015-02-12 11:02:30 -0800 | [diff] [blame] | 29 | static ssize_t ReadFullyWithRead(void *ctx, void *buf, size_t count) |
| 30 | { |
| 31 | ssize_t nr_read = 0; |
| 32 | int fd = *((int*)ctx); |
| 33 | while (nr_read < count) { |
| 34 | ssize_t to_read = count - nr_read; |
| 35 | ssize_t chunk = read(fd, buf + nr_read, to_read); |
| 36 | if (chunk < 0) { |
| 37 | return -1; |
| 38 | } else if (chunk == 0) { |
| 39 | break; |
| 40 | } |
| 41 | nr_read += chunk; |
| 42 | } |
| 43 | return nr_read; |
| 44 | } |
| 45 | |
Nam T. Nguyen | f44ebbe | 2015-02-12 11:10:35 -0800 | [diff] [blame] | 46 | #ifdef USE_MTD |
| 47 | static ssize_t ReadFullyWithMtdRead(void *ctx, void *buf, size_t count) |
| 48 | { |
| 49 | MtdReadContext *mtd_ctx = (MtdReadContext*)ctx; |
| 50 | return mtd_read_data(mtd_ctx, buf, count); |
| 51 | } |
| 52 | #endif |
| 53 | |
Nam T. Nguyen | aa4ee34 | 2015-02-12 11:02:30 -0800 | [diff] [blame] | 54 | /* Skip the stream by calling |read_fn| many times. Return 0 on success. */ |
| 55 | static int SkipWithRead(void *ctx, ReadFullyFn read_fn, size_t count) |
| 56 | { |
| 57 | char buf[1024]; |
| 58 | ssize_t nr_skipped = 0; |
| 59 | while (nr_skipped < count) { |
| 60 | ssize_t to_read = count - nr_skipped; |
| 61 | if (to_read > sizeof(buf)) { |
| 62 | to_read = sizeof(buf); |
| 63 | } |
| 64 | if (read_fn(ctx, buf, to_read) != to_read) { |
| 65 | return -1; |
| 66 | } |
| 67 | nr_skipped += to_read; |
| 68 | } |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | static char *FindKernelConfigFromStream(void *ctx, ReadFullyFn read_fn, |
| 73 | uint64_t kernel_body_load_address) |
| 74 | { |
| 75 | VbKeyBlockHeader key_block; |
| 76 | VbKernelPreambleHeader preamble; |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 77 | uint32_t now = 0; |
| 78 | uint32_t offset = 0; |
Randall Spangler | 17f8d34 | 2013-01-11 10:55:11 -0800 | [diff] [blame] | 79 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 80 | /* Skip the key block */ |
Nam T. Nguyen | aa4ee34 | 2015-02-12 11:02:30 -0800 | [diff] [blame] | 81 | if (read_fn(ctx, &key_block, sizeof(key_block)) != sizeof(key_block)) { |
| 82 | VbExError("not enough data to fill key block header\n"); |
| 83 | return NULL; |
| 84 | } |
| 85 | ssize_t to_skip = key_block.key_block_size - sizeof(key_block); |
| 86 | if (to_skip < 0 || SkipWithRead(ctx, read_fn, to_skip)) { |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 87 | VbExError("key_block_size advances past the end of the blob\n"); |
| 88 | return NULL; |
| 89 | } |
Nam T. Nguyen | aa4ee34 | 2015-02-12 11:02:30 -0800 | [diff] [blame] | 90 | now += key_block.key_block_size; |
Randall Spangler | 17f8d34 | 2013-01-11 10:55:11 -0800 | [diff] [blame] | 91 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 92 | /* Open up the preamble */ |
Nam T. Nguyen | aa4ee34 | 2015-02-12 11:02:30 -0800 | [diff] [blame] | 93 | if (read_fn(ctx, &preamble, sizeof(preamble)) != sizeof(preamble)) { |
| 94 | VbExError("not enough data to fill preamble\n"); |
| 95 | return NULL; |
| 96 | } |
| 97 | to_skip = preamble.preamble_size - sizeof(preamble); |
| 98 | if (to_skip < 0 || SkipWithRead(ctx, read_fn, to_skip)) { |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 99 | VbExError("preamble_size advances past the end of the blob\n"); |
| 100 | return NULL; |
| 101 | } |
Nam T. Nguyen | aa4ee34 | 2015-02-12 11:02:30 -0800 | [diff] [blame] | 102 | now += preamble.preamble_size; |
Randall Spangler | 17f8d34 | 2013-01-11 10:55:11 -0800 | [diff] [blame] | 103 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 104 | /* Read body_load_address from preamble if no |
| 105 | * kernel_body_load_address */ |
| 106 | if (kernel_body_load_address == USE_PREAMBLE_LOAD_ADDR) |
Nam T. Nguyen | aa4ee34 | 2015-02-12 11:02:30 -0800 | [diff] [blame] | 107 | kernel_body_load_address = preamble.body_load_address; |
Randall Spangler | 17f8d34 | 2013-01-11 10:55:11 -0800 | [diff] [blame] | 108 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 109 | /* The x86 kernels have a pointer to the kernel commandline in the |
| 110 | * zeropage table, but that's irrelevant for ARM. Both types keep the |
| 111 | * config blob in the same place, so just go find it. */ |
Nam T. Nguyen | aa4ee34 | 2015-02-12 11:02:30 -0800 | [diff] [blame] | 112 | offset = preamble.bootloader_address - |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 113 | (kernel_body_load_address + CROS_PARAMS_SIZE + |
| 114 | CROS_CONFIG_SIZE) + now; |
Nam T. Nguyen | aa4ee34 | 2015-02-12 11:02:30 -0800 | [diff] [blame] | 115 | to_skip = offset - now; |
| 116 | if (to_skip < 0 || SkipWithRead(ctx, read_fn, to_skip)) { |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 117 | VbExError("params are outside of the memory blob: %x\n", |
| 118 | offset); |
| 119 | return NULL; |
| 120 | } |
Nam T. Nguyen | aa4ee34 | 2015-02-12 11:02:30 -0800 | [diff] [blame] | 121 | char *ret = malloc(CROS_CONFIG_SIZE); |
| 122 | if (!ret) { |
| 123 | VbExError("No memory\n"); |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 124 | return NULL; |
| 125 | } |
Nam T. Nguyen | aa4ee34 | 2015-02-12 11:02:30 -0800 | [diff] [blame] | 126 | if (read_fn(ctx, ret, CROS_CONFIG_SIZE) != CROS_CONFIG_SIZE) { |
| 127 | VbExError("Cannot read kernel config\n"); |
| 128 | free(ret); |
| 129 | ret = NULL; |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 130 | } |
Nam T. Nguyen | aa4ee34 | 2015-02-12 11:02:30 -0800 | [diff] [blame] | 131 | return ret; |
Randall Spangler | 17f8d34 | 2013-01-11 10:55:11 -0800 | [diff] [blame] | 132 | } |
Bill Richardson | 28b65ca | 2013-03-28 17:31:24 -0700 | [diff] [blame] | 133 | |
Bill Richardson | 28b65ca | 2013-03-28 17:31:24 -0700 | [diff] [blame] | 134 | char *FindKernelConfig(const char *infile, uint64_t kernel_body_load_address) |
| 135 | { |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 136 | char *newstr = NULL; |
Bill Richardson | 28b65ca | 2013-03-28 17:31:24 -0700 | [diff] [blame] | 137 | |
Nam T. Nguyen | aa4ee34 | 2015-02-12 11:02:30 -0800 | [diff] [blame] | 138 | int fd = open(infile, O_RDONLY | O_CLOEXEC | O_LARGEFILE); |
| 139 | if (fd < 0) { |
| 140 | VbExError("Cannot open %s\n", infile); |
| 141 | return NULL; |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 142 | } |
Bill Richardson | 28b65ca | 2013-03-28 17:31:24 -0700 | [diff] [blame] | 143 | |
Nam T. Nguyen | aa4ee34 | 2015-02-12 11:02:30 -0800 | [diff] [blame] | 144 | void *ctx = &fd; |
| 145 | ReadFullyFn read_fn = ReadFullyWithRead; |
Bill Richardson | 28b65ca | 2013-03-28 17:31:24 -0700 | [diff] [blame] | 146 | |
Nam T. Nguyen | f44ebbe | 2015-02-12 11:10:35 -0800 | [diff] [blame] | 147 | #ifdef USE_MTD |
| 148 | struct stat stat_buf; |
| 149 | if (fstat(fd, &stat_buf)) { |
| 150 | VbExError("Cannot stat %s\n", infile); |
| 151 | return NULL; |
| 152 | } |
| 153 | |
| 154 | int is_mtd = (major(stat_buf.st_rdev) == MTD_CHAR_MAJOR); |
| 155 | if (is_mtd) { |
| 156 | ctx = mtd_read_descriptor(fd, infile); |
| 157 | if (!ctx) { |
| 158 | VbExError("Cannot read from MTD device %s\n", infile); |
| 159 | return NULL; |
| 160 | } |
| 161 | read_fn = ReadFullyWithMtdRead; |
| 162 | } |
| 163 | #endif |
| 164 | |
Nam T. Nguyen | aa4ee34 | 2015-02-12 11:02:30 -0800 | [diff] [blame] | 165 | newstr = FindKernelConfigFromStream(ctx, read_fn, |
| 166 | kernel_body_load_address); |
Bill Richardson | 28b65ca | 2013-03-28 17:31:24 -0700 | [diff] [blame] | 167 | |
Nam T. Nguyen | f44ebbe | 2015-02-12 11:10:35 -0800 | [diff] [blame] | 168 | #ifdef USE_MTD |
| 169 | if (is_mtd) { |
| 170 | mtd_read_close(ctx); |
| 171 | } |
| 172 | #endif |
Nam T. Nguyen | aa4ee34 | 2015-02-12 11:02:30 -0800 | [diff] [blame] | 173 | close(fd); |
Bill Richardson | 28b65ca | 2013-03-28 17:31:24 -0700 | [diff] [blame] | 174 | |
Bill Richardson | 31d95c2 | 2014-08-24 22:07:17 -0700 | [diff] [blame] | 175 | return newstr; |
Bill Richardson | 28b65ca | 2013-03-28 17:31:24 -0700 | [diff] [blame] | 176 | } |