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