blob: d44b7fa4e6162632e594d141193a6894d4c6c513 [file] [log] [blame]
Arthur Heymansc82900b2018-01-10 12:48:16 +01001/*
2 * Copyright 2015, Google Inc.
3 * Copyright 2018-present, Facebook Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * Alternatively, this software may be distributed under the terms of the
33 * GNU General Public License ("GPL") version 2 as published by the Free
34 * Software Foundation.
35 */
36
37#include <ctype.h>
38#include <stdlib.h>
39#include <string.h>
40#include <sys/types.h>
41#include "flash.h"
42#include "fmap.h"
43
44static size_t fmap_size(const struct fmap *fmap)
45{
46 return sizeof(*fmap) + (fmap->nareas * sizeof(struct fmap_area));
47}
48
49static int is_valid_fmap(const struct fmap *fmap)
50{
51 if (memcmp(fmap, FMAP_SIGNATURE, strlen(FMAP_SIGNATURE)) != 0)
52 return 0;
53 /* strings containing the magic tend to fail here */
54 if (fmap->ver_major > FMAP_VER_MAJOR)
55 return 0;
56 if (fmap->ver_minor > FMAP_VER_MINOR)
57 return 0;
58 /* a basic consistency check: flash address space size should be larger
59 * than the size of the fmap data structure */
60 if (fmap->size < fmap_size(fmap))
61 return 0;
62
63 /* fmap-alikes along binary data tend to fail on having a valid,
64 * null-terminated string in the name field.*/
65 int i;
66 for (i = 0; i < FMAP_STRLEN; i++) {
67 if (fmap->name[i] == 0)
68 break;
69 if (!isgraph(fmap->name[i]))
70 return 0;
71 if (i == FMAP_STRLEN - 1) {
72 /* name is specified to be null terminated single-word string
73 * without spaces. We did not break in the 0 test, we know it
74 * is a printable spaceless string but we're seeing FMAP_STRLEN
75 * symbols, which is one too many.
76 */
77 return 0;
78 }
79 }
80 return 1;
81
82}
83
84/**
85 * @brief Do a brute-force linear search for fmap in provided buffer
86 *
87 * @param[in] buffer The buffer to search
88 * @param[in] len Length (in bytes) to search
89 *
90 * @return offset in buffer where fmap is found if successful
91 * -1 to indicate that fmap was not found
92 * -2 to indicate fmap is truncated or exceeds buffer + len
93 */
94static off_t fmap_lsearch(const uint8_t *buf, size_t len)
95{
96 off_t offset;
97 bool fmap_found = 0;
98
99 for (offset = 0; offset <= len - sizeof(struct fmap); offset++) {
100 if (is_valid_fmap((struct fmap *)&buf[offset])) {
101 fmap_found = 1;
102 break;
103 }
104 }
105
106 if (!fmap_found)
107 return -1;
108
109 if (offset + fmap_size((struct fmap *)&buf[offset]) > len) {
110 msg_gerr("fmap size exceeds buffer boundary.\n");
111 return -2;
112 }
113
114 return offset;
115}
116
117/**
118 * @brief Read fmap from provided buffer and copy it to fmap_out
119 *
120 * @param[out] fmap_out Double-pointer to location to store fmap contents.
121 * Caller must free allocated fmap contents.
122 * @param[in] buf Buffer to search
123 * @param[in] len Length (in bytes) to search
124 *
125 * @return 0 if successful
126 * 1 to indicate error
127 * 2 to indicate fmap is not found
128 */
129int fmap_read_from_buffer(struct fmap **fmap_out, const uint8_t *const buf, size_t len)
130{
131 off_t offset = fmap_lsearch(buf, len);
132 if (offset < 0) {
133 msg_gdbg("Unable to find fmap in provided buffer.\n");
134 return 2;
135 } else {
136 msg_gdbg("Found fmap at offset 0x%06zx\n", (size_t)offset);
137 }
138
139 const struct fmap *fmap = (const struct fmap *)(buf + offset);
140 *fmap_out = malloc(fmap_size(fmap));
141 if (*fmap_out == NULL) {
142 msg_gerr("Out of memory.\n");
143 return 1;
144 }
145
146 memcpy(*fmap_out, fmap, fmap_size(fmap));
147 return 0;
148}
149
150static int fmap_lsearch_rom(struct fmap **fmap_out,
151 struct flashctx *const flashctx, size_t rom_offset, size_t len)
152{
153 int ret = -1;
154 uint8_t *buf;
155
156 if (prepare_flash_access(flashctx, true, false, false, false))
157 goto _finalize_ret;
158
159 /* likely more memory than we need, but it simplifies handling and
160 * printing offsets to keep them uniform with what's on the ROM */
161 buf = malloc(rom_offset + len);
162 if (!buf) {
163 msg_gerr("Out of memory.\n");
164 goto _finalize_ret;
165 }
166
167 ret = flashctx->chip->read(flashctx, buf + rom_offset, rom_offset, len);
168 if (ret) {
169 msg_pdbg("Cannot read ROM contents.\n");
170 goto _free_ret;
171 }
172
173 ret = fmap_read_from_buffer(fmap_out, buf + rom_offset, len);
174_free_ret:
175 free(buf);
176_finalize_ret:
177 finalize_flash_access(flashctx);
178 return ret;
179}
180
181static int fmap_bsearch_rom(struct fmap **fmap_out, struct flashctx *const flashctx,
182 size_t rom_offset, size_t len, int min_stride)
183{
184 size_t stride, fmap_len = 0;
185 int ret = 1, fmap_found = 0, check_offset_0 = 1;
186 struct fmap *fmap;
187 const unsigned int chip_size = flashctx->chip->total_size * 1024;
188 const int sig_len = strlen(FMAP_SIGNATURE);
189
190 if (rom_offset + len > flashctx->chip->total_size * 1024)
191 return 1;
192
193 if (len < sizeof(*fmap))
194 return 1;
195
196 if (prepare_flash_access(flashctx, true, false, false, false))
197 return 1;
198
199 fmap = malloc(sizeof(struct fmap));
200 if (!fmap) {
201 msg_gerr("Out of memory.\n");
202 goto _free_ret;
203 }
204
205 /*
206 * For efficient operation, we start with the largest stride possible
207 * and then decrease the stride on each iteration. Also, check for a
208 * remainder when modding the offset with the previous stride. This
209 * makes it so that each offset is only checked once.
210 *
211 * Zero (rom_offset == 0) is a special case and is handled using a
212 * variable to track whether or not we've checked it.
213 */
214 size_t offset;
215 for (stride = chip_size / 2; stride >= min_stride; stride /= 2) {
216 if (stride > len)
217 continue;
218
219 for (offset = rom_offset;
220 offset <= rom_offset + len - sizeof(struct fmap);
221 offset += stride) {
222 if ((offset % (stride * 2) == 0) && (offset != 0))
223 continue;
224 if (offset == 0 && !check_offset_0)
225 continue;
226 check_offset_0 = 0;
227
228 /* Read errors are considered non-fatal since we may
229 * encounter locked regions and want to continue. */
230 if (flashctx->chip->read(flashctx, (uint8_t *)fmap, offset, sig_len)) {
231 /*
232 * Print in verbose mode only to avoid excessive
233 * messages for benign errors. Subsequent error
234 * prints should be done as usual.
235 */
236 msg_cdbg("Cannot read %d bytes at offset %zu\n", sig_len, offset);
237 continue;
238 }
239
240 if (memcmp(fmap, FMAP_SIGNATURE, sig_len) != 0)
241 continue;
242
243 if (flashctx->chip->read(flashctx, (uint8_t *)fmap + sig_len,
244 offset + sig_len, sizeof(*fmap) - sig_len)) {
245 msg_cerr("Cannot read %zu bytes at offset %06zx\n",
246 sizeof(*fmap) + sig_len, offset + sig_len);
247 continue;
248 }
249
250 if (is_valid_fmap(fmap)) {
251 msg_gdbg("fmap found at offset 0x%06zx\n", offset);
252 fmap_found = 1;
253 break;
254 } else {
255 msg_gerr("fmap signature found at %zu but header is invalid.\n", offset);
256 ret = 2;
257 }
258 }
259
260 if (fmap_found)
261 break;
262 }
263
264 if (!fmap_found)
265 goto _free_ret;
266
267 fmap_len = fmap_size(fmap);
268 struct fmap *tmp = fmap;
269 fmap = realloc(fmap, fmap_len);
270 if (!fmap) {
271 msg_gerr("Failed to realloc.\n");
272 free(tmp);
273 goto _free_ret;
274 }
275
276 if (flashctx->chip->read(flashctx, (uint8_t *)fmap + sizeof(*fmap),
277 offset + sizeof(*fmap), fmap_len - sizeof(*fmap))) {
278 msg_cerr("Cannot read %zu bytes at offset %06zx\n",
279 fmap_len - sizeof(*fmap), offset + sizeof(*fmap));
280 /* Treat read failure to be fatal since this
281 * should be a valid, usable fmap. */
282 ret = 2;
283 goto _free_ret;
284 }
285
286 *fmap_out = fmap;
287 ret = 0;
288_free_ret:
289 if (ret)
290 free(fmap);
291 finalize_flash_access(flashctx);
292 return ret;
293}
294
295/**
296 * @brief Read fmap from ROM
297 *
298 * @param[out] fmap_out Double-pointer to location to store fmap contents.
299 * Caller must free allocated fmap contents.
300 * @param[in] flashctx Flash context
301 * @param[in] rom_offset Offset in ROM to begin search
302 * @param[in] len Length to search relative to rom_offset
303 *
304 * @return 0 on success,
305 * 2 if the fmap couldn't be read,
306 * 1 on any other error.
307 */
308int fmap_read_from_rom(struct fmap **fmap_out,
309 struct flashctx *const flashctx, size_t rom_offset, size_t len)
310{
311 int ret;
312
313 if (!flashctx || !flashctx->chip)
314 return 1;
315
316 /*
317 * Binary search is used at first to see if we can find an fmap quickly
318 * in a usual location (often at a power-of-2 offset). However, once we
319 * reach a small enough stride the transaction overhead will reverse the
320 * speed benefit of using bsearch at which point we need to use brute-
321 * force instead.
322 *
323 * TODO: Since flashrom is often used with high-latency external
324 * programmers we should not be overly aggressive with bsearch.
325 */
326 ret = fmap_bsearch_rom(fmap_out, flashctx, rom_offset, len, 256);
327 if (ret) {
328 msg_gdbg("Binary search failed, trying linear search...\n");
329 ret = fmap_lsearch_rom(fmap_out, flashctx, rom_offset, len);
330 }
331
332 return ret;
333}