blob: 0e8dd951b8a4351d5906fc72f64e96d4cc1a8473 [file] [log] [blame]
Arthur Heymansc82900b2018-01-10 12:48:16 +01001/*
Nikolai Artemiev354c9d92020-10-15 13:47:56 +11002 * Copyright 2010, Google LLC.
Arthur Heymansc82900b2018-01-10 12:48:16 +01003 * 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>
Felix Singer08557282022-08-19 03:19:06 +020038#include <stdbool.h>
Arthur Heymansc82900b2018-01-10 12:48:16 +010039#include <stdlib.h>
40#include <string.h>
41#include <sys/types.h>
42#include "flash.h"
43#include "fmap.h"
44
45static size_t fmap_size(const struct fmap *fmap)
46{
47 return sizeof(*fmap) + (fmap->nareas * sizeof(struct fmap_area));
48}
49
50static int is_valid_fmap(const struct fmap *fmap)
51{
52 if (memcmp(fmap, FMAP_SIGNATURE, strlen(FMAP_SIGNATURE)) != 0)
53 return 0;
54 /* strings containing the magic tend to fail here */
55 if (fmap->ver_major > FMAP_VER_MAJOR)
56 return 0;
57 if (fmap->ver_minor > FMAP_VER_MINOR)
58 return 0;
59 /* a basic consistency check: flash address space size should be larger
60 * than the size of the fmap data structure */
61 if (fmap->size < fmap_size(fmap))
62 return 0;
63
64 /* fmap-alikes along binary data tend to fail on having a valid,
65 * null-terminated string in the name field.*/
66 int i;
67 for (i = 0; i < FMAP_STRLEN; i++) {
68 if (fmap->name[i] == 0)
69 break;
70 if (!isgraph(fmap->name[i]))
71 return 0;
72 if (i == FMAP_STRLEN - 1) {
73 /* name is specified to be null terminated single-word string
74 * without spaces. We did not break in the 0 test, we know it
75 * is a printable spaceless string but we're seeing FMAP_STRLEN
76 * symbols, which is one too many.
77 */
78 return 0;
79 }
80 }
81 return 1;
82
83}
84
85/**
86 * @brief Do a brute-force linear search for fmap in provided buffer
87 *
88 * @param[in] buffer The buffer to search
89 * @param[in] len Length (in bytes) to search
90 *
91 * @return offset in buffer where fmap is found if successful
92 * -1 to indicate that fmap was not found
93 * -2 to indicate fmap is truncated or exceeds buffer + len
94 */
Julius Werner8f0db7d2022-02-14 17:07:39 -080095static ssize_t fmap_lsearch(const uint8_t *buf, size_t len)
Arthur Heymansc82900b2018-01-10 12:48:16 +010096{
Julius Werner8f0db7d2022-02-14 17:07:39 -080097 ssize_t offset;
Felix Singer08557282022-08-19 03:19:06 +020098 bool fmap_found = false;
Arthur Heymansc82900b2018-01-10 12:48:16 +010099
Edward O'Callaghan672bdcf2022-02-02 17:15:05 +1100100 if (len < sizeof(struct fmap))
101 return -1;
102
Julius Werner8f0db7d2022-02-14 17:07:39 -0800103 for (offset = 0; offset <= (ssize_t)(len - sizeof(struct fmap)); offset++) {
Arthur Heymansc82900b2018-01-10 12:48:16 +0100104 if (is_valid_fmap((struct fmap *)&buf[offset])) {
Felix Singer08557282022-08-19 03:19:06 +0200105 fmap_found = true;
Arthur Heymansc82900b2018-01-10 12:48:16 +0100106 break;
107 }
108 }
109
110 if (!fmap_found)
111 return -1;
112
113 if (offset + fmap_size((struct fmap *)&buf[offset]) > len) {
114 msg_gerr("fmap size exceeds buffer boundary.\n");
115 return -2;
116 }
117
118 return offset;
119}
120
121/**
122 * @brief Read fmap from provided buffer and copy it to fmap_out
123 *
124 * @param[out] fmap_out Double-pointer to location to store fmap contents.
125 * Caller must free allocated fmap contents.
126 * @param[in] buf Buffer to search
127 * @param[in] len Length (in bytes) to search
128 *
129 * @return 0 if successful
130 * 1 to indicate error
131 * 2 to indicate fmap is not found
132 */
133int fmap_read_from_buffer(struct fmap **fmap_out, const uint8_t *const buf, size_t len)
134{
Julius Werner8f0db7d2022-02-14 17:07:39 -0800135 ssize_t offset = fmap_lsearch(buf, len);
Arthur Heymansc82900b2018-01-10 12:48:16 +0100136 if (offset < 0) {
137 msg_gdbg("Unable to find fmap in provided buffer.\n");
138 return 2;
Arthur Heymansc82900b2018-01-10 12:48:16 +0100139 }
Elyes HAOUASe2764112019-06-09 17:09:25 +0200140 msg_gdbg("Found fmap at offset 0x%06zx\n", (size_t)offset);
Arthur Heymansc82900b2018-01-10 12:48:16 +0100141
142 const struct fmap *fmap = (const struct fmap *)(buf + offset);
143 *fmap_out = malloc(fmap_size(fmap));
144 if (*fmap_out == NULL) {
145 msg_gerr("Out of memory.\n");
146 return 1;
147 }
148
149 memcpy(*fmap_out, fmap, fmap_size(fmap));
150 return 0;
151}
152
153static int fmap_lsearch_rom(struct fmap **fmap_out,
154 struct flashctx *const flashctx, size_t rom_offset, size_t len)
155{
156 int ret = -1;
157 uint8_t *buf;
158
159 if (prepare_flash_access(flashctx, true, false, false, false))
160 goto _finalize_ret;
161
162 /* likely more memory than we need, but it simplifies handling and
163 * printing offsets to keep them uniform with what's on the ROM */
164 buf = malloc(rom_offset + len);
165 if (!buf) {
166 msg_gerr("Out of memory.\n");
167 goto _finalize_ret;
168 }
169
Richard Hughes842d6782021-01-15 09:48:12 +0000170 ret = flashprog_read_range(flashctx, buf + rom_offset, rom_offset, len);
Arthur Heymansc82900b2018-01-10 12:48:16 +0100171 if (ret) {
172 msg_pdbg("Cannot read ROM contents.\n");
173 goto _free_ret;
174 }
175
176 ret = fmap_read_from_buffer(fmap_out, buf + rom_offset, len);
177_free_ret:
178 free(buf);
179_finalize_ret:
180 finalize_flash_access(flashctx);
181 return ret;
182}
183
184static int fmap_bsearch_rom(struct fmap **fmap_out, struct flashctx *const flashctx,
Nico Huber519be662018-12-23 20:03:35 +0100185 size_t rom_offset, size_t len, size_t min_stride)
Arthur Heymansc82900b2018-01-10 12:48:16 +0100186{
187 size_t stride, fmap_len = 0;
Felix Singer08557282022-08-19 03:19:06 +0200188 int ret = 1;
189 bool fmap_found = false;
190 bool check_offset_0 = true;
Arthur Heymansc82900b2018-01-10 12:48:16 +0100191 struct fmap *fmap;
192 const unsigned int chip_size = flashctx->chip->total_size * 1024;
193 const int sig_len = strlen(FMAP_SIGNATURE);
194
195 if (rom_offset + len > flashctx->chip->total_size * 1024)
196 return 1;
197
198 if (len < sizeof(*fmap))
199 return 1;
200
201 if (prepare_flash_access(flashctx, true, false, false, false))
202 return 1;
203
Angel Pons690a9442021-06-07 12:33:53 +0200204 fmap = malloc(sizeof(*fmap));
Arthur Heymansc82900b2018-01-10 12:48:16 +0100205 if (!fmap) {
206 msg_gerr("Out of memory.\n");
207 goto _free_ret;
208 }
209
210 /*
211 * For efficient operation, we start with the largest stride possible
212 * and then decrease the stride on each iteration. Also, check for a
213 * remainder when modding the offset with the previous stride. This
214 * makes it so that each offset is only checked once.
215 *
216 * Zero (rom_offset == 0) is a special case and is handled using a
217 * variable to track whether or not we've checked it.
218 */
219 size_t offset;
220 for (stride = chip_size / 2; stride >= min_stride; stride /= 2) {
221 if (stride > len)
222 continue;
223
224 for (offset = rom_offset;
225 offset <= rom_offset + len - sizeof(struct fmap);
226 offset += stride) {
227 if ((offset % (stride * 2) == 0) && (offset != 0))
228 continue;
229 if (offset == 0 && !check_offset_0)
230 continue;
Felix Singer08557282022-08-19 03:19:06 +0200231 check_offset_0 = false;
Arthur Heymansc82900b2018-01-10 12:48:16 +0100232
233 /* Read errors are considered non-fatal since we may
234 * encounter locked regions and want to continue. */
Richard Hughes842d6782021-01-15 09:48:12 +0000235 if (flashprog_read_range(flashctx, (uint8_t *)fmap, offset, sig_len)) {
Arthur Heymansc82900b2018-01-10 12:48:16 +0100236 /*
237 * Print in verbose mode only to avoid excessive
238 * messages for benign errors. Subsequent error
239 * prints should be done as usual.
240 */
241 msg_cdbg("Cannot read %d bytes at offset %zu\n", sig_len, offset);
242 continue;
243 }
244
245 if (memcmp(fmap, FMAP_SIGNATURE, sig_len) != 0)
246 continue;
247
Richard Hughes842d6782021-01-15 09:48:12 +0000248 if (flashprog_read_range(flashctx, (uint8_t *)fmap + sig_len,
Arthur Heymansc82900b2018-01-10 12:48:16 +0100249 offset + sig_len, sizeof(*fmap) - sig_len)) {
250 msg_cerr("Cannot read %zu bytes at offset %06zx\n",
Nico Huberba72e912018-12-11 12:10:04 +0100251 sizeof(*fmap) - sig_len, offset + sig_len);
Arthur Heymansc82900b2018-01-10 12:48:16 +0100252 continue;
253 }
254
255 if (is_valid_fmap(fmap)) {
256 msg_gdbg("fmap found at offset 0x%06zx\n", offset);
Felix Singer08557282022-08-19 03:19:06 +0200257 fmap_found = true;
Arthur Heymansc82900b2018-01-10 12:48:16 +0100258 break;
Arthur Heymansc82900b2018-01-10 12:48:16 +0100259 }
Elyes HAOUASe2764112019-06-09 17:09:25 +0200260 msg_gerr("fmap signature found at %zu but header is invalid.\n", offset);
261 ret = 2;
Arthur Heymansc82900b2018-01-10 12:48:16 +0100262 }
263
264 if (fmap_found)
265 break;
266 }
267
268 if (!fmap_found)
269 goto _free_ret;
270
271 fmap_len = fmap_size(fmap);
272 struct fmap *tmp = fmap;
273 fmap = realloc(fmap, fmap_len);
274 if (!fmap) {
275 msg_gerr("Failed to realloc.\n");
276 free(tmp);
277 goto _free_ret;
278 }
279
Richard Hughes842d6782021-01-15 09:48:12 +0000280 if (flashprog_read_range(flashctx, (uint8_t *)fmap + sizeof(*fmap),
Arthur Heymansc82900b2018-01-10 12:48:16 +0100281 offset + sizeof(*fmap), fmap_len - sizeof(*fmap))) {
282 msg_cerr("Cannot read %zu bytes at offset %06zx\n",
283 fmap_len - sizeof(*fmap), offset + sizeof(*fmap));
284 /* Treat read failure to be fatal since this
285 * should be a valid, usable fmap. */
286 ret = 2;
287 goto _free_ret;
288 }
289
290 *fmap_out = fmap;
291 ret = 0;
292_free_ret:
293 if (ret)
294 free(fmap);
295 finalize_flash_access(flashctx);
296 return ret;
297}
298
299/**
300 * @brief Read fmap from ROM
301 *
302 * @param[out] fmap_out Double-pointer to location to store fmap contents.
303 * Caller must free allocated fmap contents.
304 * @param[in] flashctx Flash context
305 * @param[in] rom_offset Offset in ROM to begin search
306 * @param[in] len Length to search relative to rom_offset
307 *
308 * @return 0 on success,
309 * 2 if the fmap couldn't be read,
310 * 1 on any other error.
311 */
312int fmap_read_from_rom(struct fmap **fmap_out,
313 struct flashctx *const flashctx, size_t rom_offset, size_t len)
314{
315 int ret;
316
317 if (!flashctx || !flashctx->chip)
318 return 1;
319
320 /*
321 * Binary search is used at first to see if we can find an fmap quickly
322 * in a usual location (often at a power-of-2 offset). However, once we
323 * reach a small enough stride the transaction overhead will reverse the
324 * speed benefit of using bsearch at which point we need to use brute-
325 * force instead.
326 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200327 * TODO: Since flashprog is often used with high-latency external
Arthur Heymansc82900b2018-01-10 12:48:16 +0100328 * programmers we should not be overly aggressive with bsearch.
329 */
330 ret = fmap_bsearch_rom(fmap_out, flashctx, rom_offset, len, 256);
331 if (ret) {
332 msg_gdbg("Binary search failed, trying linear search...\n");
333 ret = fmap_lsearch_rom(fmap_out, flashctx, rom_offset, len);
334 }
335
336 return ret;
337}