blob: c2d59433ba71579ad7c36d5365f7f87762bc7fd7 [file] [log] [blame]
Bill Richardson6f396152014-07-15 12:52:19 -07001/* Copyright 2012 The Chromium OS Authors. All rights reserved.
Randall Spangler17f8d342013-01-11 10:55:11 -08002 * 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 Richardson28b65ca2013-03-28 17:31:24 -07009#include <string.h>
Randall Spangler17f8d342013-01-11 10:55:11 -080010#include <sys/mman.h>
Nam T. Nguyenaa4ee342015-02-12 11:02:30 -080011#include <sys/fcntl.h>
12#include <sys/stat.h>
13#include <sys/types.h>
14#include <unistd.h>
Randall Spangler17f8d342013-01-11 10:55:11 -080015
Randall Spangler17f8d342013-01-11 10:55:11 -080016#include "host_common.h"
17#include "kernel_blob.h"
18#include "vboot_api.h"
Bill Richardson0c3ba242013-03-29 11:09:30 -070019#include "vboot_host.h"
Randall Spangler17f8d342013-01-11 10:55:11 -080020
Nam T. Nguyenf44ebbe2015-02-12 11:10:35 -080021#ifdef USE_MTD
22#include <linux/major.h>
23#include <mtd/mtd-user.h>
24#include <mtdutils.h>
25#endif
26
Nam T. Nguyenaa4ee342015-02-12 11:02:30 -080027typedef ssize_t (*ReadFullyFn)(void *ctx, void *buf, size_t count);
Randall Spangler17f8d342013-01-11 10:55:11 -080028
Nam T. Nguyenaa4ee342015-02-12 11:02:30 -080029static 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. Nguyenf44ebbe2015-02-12 11:10:35 -080046#ifdef USE_MTD
47static 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. Nguyenaa4ee342015-02-12 11:02:30 -080054/* Skip the stream by calling |read_fn| many times. Return 0 on success. */
55static 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
72static char *FindKernelConfigFromStream(void *ctx, ReadFullyFn read_fn,
73 uint64_t kernel_body_load_address)
74{
75 VbKeyBlockHeader key_block;
76 VbKernelPreambleHeader preamble;
Bill Richardson31d95c22014-08-24 22:07:17 -070077 uint32_t now = 0;
78 uint32_t offset = 0;
Randall Spangler17f8d342013-01-11 10:55:11 -080079
Bill Richardson31d95c22014-08-24 22:07:17 -070080 /* Skip the key block */
Nam T. Nguyenaa4ee342015-02-12 11:02:30 -080081 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 Richardson31d95c22014-08-24 22:07:17 -070087 VbExError("key_block_size advances past the end of the blob\n");
88 return NULL;
89 }
Nam T. Nguyenaa4ee342015-02-12 11:02:30 -080090 now += key_block.key_block_size;
Randall Spangler17f8d342013-01-11 10:55:11 -080091
Bill Richardson31d95c22014-08-24 22:07:17 -070092 /* Open up the preamble */
Nam T. Nguyenaa4ee342015-02-12 11:02:30 -080093 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 Richardson31d95c22014-08-24 22:07:17 -070099 VbExError("preamble_size advances past the end of the blob\n");
100 return NULL;
101 }
Nam T. Nguyenaa4ee342015-02-12 11:02:30 -0800102 now += preamble.preamble_size;
Randall Spangler17f8d342013-01-11 10:55:11 -0800103
Bill Richardson31d95c22014-08-24 22:07:17 -0700104 /* 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. Nguyenaa4ee342015-02-12 11:02:30 -0800107 kernel_body_load_address = preamble.body_load_address;
Randall Spangler17f8d342013-01-11 10:55:11 -0800108
Bill Richardson31d95c22014-08-24 22:07:17 -0700109 /* 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. Nguyenaa4ee342015-02-12 11:02:30 -0800112 offset = preamble.bootloader_address -
Bill Richardson31d95c22014-08-24 22:07:17 -0700113 (kernel_body_load_address + CROS_PARAMS_SIZE +
114 CROS_CONFIG_SIZE) + now;
Nam T. Nguyenaa4ee342015-02-12 11:02:30 -0800115 to_skip = offset - now;
116 if (to_skip < 0 || SkipWithRead(ctx, read_fn, to_skip)) {
Bill Richardson31d95c22014-08-24 22:07:17 -0700117 VbExError("params are outside of the memory blob: %x\n",
118 offset);
119 return NULL;
120 }
Nam T. Nguyenaa4ee342015-02-12 11:02:30 -0800121 char *ret = malloc(CROS_CONFIG_SIZE);
122 if (!ret) {
123 VbExError("No memory\n");
Bill Richardson31d95c22014-08-24 22:07:17 -0700124 return NULL;
125 }
Nam T. Nguyenaa4ee342015-02-12 11:02:30 -0800126 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 Richardson31d95c22014-08-24 22:07:17 -0700130 }
Nam T. Nguyenaa4ee342015-02-12 11:02:30 -0800131 return ret;
Randall Spangler17f8d342013-01-11 10:55:11 -0800132}
Bill Richardson28b65ca2013-03-28 17:31:24 -0700133
Bill Richardson28b65ca2013-03-28 17:31:24 -0700134char *FindKernelConfig(const char *infile, uint64_t kernel_body_load_address)
135{
Bill Richardson31d95c22014-08-24 22:07:17 -0700136 char *newstr = NULL;
Bill Richardson28b65ca2013-03-28 17:31:24 -0700137
Nam T. Nguyenaa4ee342015-02-12 11:02:30 -0800138 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 Richardson31d95c22014-08-24 22:07:17 -0700142 }
Bill Richardson28b65ca2013-03-28 17:31:24 -0700143
Nam T. Nguyenaa4ee342015-02-12 11:02:30 -0800144 void *ctx = &fd;
145 ReadFullyFn read_fn = ReadFullyWithRead;
Bill Richardson28b65ca2013-03-28 17:31:24 -0700146
Nam T. Nguyenf44ebbe2015-02-12 11:10:35 -0800147#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. Nguyenaa4ee342015-02-12 11:02:30 -0800165 newstr = FindKernelConfigFromStream(ctx, read_fn,
166 kernel_body_load_address);
Bill Richardson28b65ca2013-03-28 17:31:24 -0700167
Nam T. Nguyenf44ebbe2015-02-12 11:10:35 -0800168#ifdef USE_MTD
169 if (is_mtd) {
170 mtd_read_close(ctx);
171 }
172#endif
Nam T. Nguyenaa4ee342015-02-12 11:02:30 -0800173 close(fd);
Bill Richardson28b65ca2013-03-28 17:31:24 -0700174
Bill Richardson31d95c22014-08-24 22:07:17 -0700175 return newstr;
Bill Richardson28b65ca2013-03-28 17:31:24 -0700176}