blob: 7e12c3ec30eb1017cba031f30bb3cf0e26089a49 [file] [log] [blame]
Stefan Tauner6ad6e012014-06-12 00:04:32 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2009-2010 Carl-Daniel Hailfinger
5 * Copyright (C) 2013 Stefan Tauner
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
Stefan Tauner6ad6e012014-06-12 00:04:32 +000016 */
17
18#include <ctype.h>
19#include <stdlib.h>
20#include <string.h>
Nico Huberbe42cf22026-02-21 17:40:44 +010021
Stefan Tauner6ad6e012014-06-12 00:04:32 +000022#include "flash.h"
Nico Huber450058c2026-07-04 15:29:28 +020023#include "layout.h"
Nico Huberbe42cf22026-02-21 17:40:44 +010024#include "programmer.h"
Stefan Tauner6ad6e012014-06-12 00:04:32 +000025
Nico Huberaf9d7382023-05-01 13:33:26 +020026/* Check if raw data is all 0 or all 1. */
27bool flashprog_no_data(const void *const raw_data, const size_t len)
28{
29 const uint8_t *const raw_end = (const uint8_t *)raw_data + len;
30 const uint8_t patterns[] = { 0x00, 0xff };
31 size_t i;
32
33 for (i = 0; i < ARRAY_SIZE(patterns); ++i) {
34 const uint8_t *raw_ptr;
35 for (raw_ptr = raw_data; raw_ptr < raw_end; ++raw_ptr) {
36 if (*raw_ptr != patterns[i])
37 break;
38 }
39 if (raw_ptr == raw_end)
40 return true;
41 }
42
43 return false;
44}
45
Nico Huber7679b5c2023-04-28 21:48:53 +000046int flashprog_read_chunked(struct flashctx *const flash, uint8_t *dst, unsigned int start, unsigned int len,
Nico Huber044c9dc2023-12-29 23:26:57 +010047 unsigned int chunksize, readfunc_t *const read)
Nico Huber7679b5c2023-04-28 21:48:53 +000048{
49 int ret;
50 size_t to_read;
Nico Huber044c9dc2023-12-29 23:26:57 +010051
52 if (chunksize > 256 && chunksize & 3)
53 chunksize &= ~3;
54
Nico Huber7679b5c2023-04-28 21:48:53 +000055 for (; len; len -= to_read, dst += to_read, start += to_read) {
56 to_read = min(chunksize, len);
57 ret = read(flash, dst, start, to_read);
58 if (ret)
59 return ret;
60 flashprog_progress_add(flash, to_read);
61 }
62 return 0;
63}
64
Nico Huberbe42cf22026-02-21 17:40:44 +010065int flashprog_limit_chip(struct flashctx *flash)
66{
67 const chipsize_t limit = flash->mst.common->max_rom_decode;
68 struct flashchip *const chip = flash->chip;
69 const chipsize_t chip_size = chip->total_size * 1024;
70 unsigned int usable_erasers = 0;
71 unsigned int i;
72
73
74 /* Chip is small enough or already limited. */
75 if (chip_size <= limit)
76 return 0;
77
Nico Huber450058c2026-07-04 15:29:28 +020078 const struct flashprog_layout *const layout = get_default_layout(flash);
79 if (layout) {
80 struct romentry *const entry = (struct romentry *)layout_next(layout, NULL);
81 if (entry)
82 entry->end = limit - 1;
83 }
84
Nico Huberbe42cf22026-02-21 17:40:44 +010085 /* Undefine all block_erasers that don't operate on the whole chip,
86 and adjust the eraseblock size of those which do. */
87 for (i = 0; i < NUM_ERASEFUNCTIONS; ++i) {
88 if (chip->block_erasers[i].eraseblocks[0].size != chip_size) {
89 chip->block_erasers[i].eraseblocks[0].count = 0;
90 chip->block_erasers[i].block_erase = NULL;
91 } else {
92 chip->block_erasers[i].eraseblocks[0].size = limit;
93 usable_erasers++;
94 }
95 }
96
97 if (usable_erasers) {
98 chip->total_size = limit / 1024;
99 if (chip->page_size > limit)
100 chip->page_size = limit;
101 return 0;
102 } else {
103 msg_pdbg("Failed to adjust size of chip \"%s\" (%d kB).\n",
104 chip->name, chip->total_size);
105 return -1;
106 }
107}
108
Nico Hubera19cd392026-07-04 15:25:27 +0200109/* Compare 64 naturally aligned bytes (often matches a cache line). */
110static int compare64(const char *const s1, const char *const s2, unsigned int offset)
111{
112 offset &= ~63;
113 return memcmp(s1 + offset, s2 + offset, 64);
114}
115
116/* Compare two memory ranges at pseudo-random offsets. */
117int compare_sparse(const void *const s1, const void *const s2, const size_t n)
118{
119 const unsigned int offsets[] = {
120 12, 123, 1234, 12345, 123456, 123456, 1234567, 12345678, 123456789,
121 0x12, 0x123, 0x1234, 0x12345, 0x123456, 0x1234567, 0x12345678,
122 0, 01, 012, 0123, 01234, 012345, 0123456, 01234567,
123 };
124 const unsigned int step = 1234;
125
126 if (n < step + 64)
127 return 0;
128
129 unsigned int i;
130 for (i = 0; i < ARRAY_SIZE(offsets); ++i) {
131 const unsigned int offset = offsets[i] % ((n - 64) / step) * step;
132
133 const int diff1 = compare64(s1, s2, offset);
134 if (diff1)
135 return diff1;
136
137 const int diff2 = compare64(s1, s2, n - 64 - offset);
138 if (diff2)
139 return diff2;
140 }
141
142 return 0;
143}
144
145/* Guesstimate the addressable size inside a memory mapping. `len' should be a power of 2. */
146size_t estimate_addressable_size(const void *const base, size_t len)
147{
148 if (len & (len - 1))
149 msg_perr("Error in %s: Given `len=%zu' is not a power of 2.\n", __func__, len);
150
151 /*
152 * We start comparing the two halves of the given space. And if
153 * they match, split the lower half, and so on until we find a
154 * mismatch (or not, in the unlikely case of empty memory?).
155 */
156 for (; len > 0; len /= 2) {
157 if (compare_sparse(base, base + len / 2, len / 2))
158 break;
159 }
160
161 return len;
162}
163
Stefan Tauner6ad6e012014-06-12 00:04:32 +0000164/* Returns the minimum number of bits needed to represent the given address.
165 * FIXME: use mind-blowing implementation. */
166uint32_t address_to_bits(uint32_t addr)
167{
168 unsigned int lzb = 0;
Daniel Campello251e3cd2022-03-16 07:05:48 -0600169 while (((1u << (31 - lzb)) & ~addr) != 0)
Stefan Tauner6ad6e012014-06-12 00:04:32 +0000170 lzb++;
171 return 32 - lzb;
172}
173
Nico Huber519be662018-12-23 20:03:35 +0100174unsigned int bitcount(unsigned long a)
Stefan Tauner6ad6e012014-06-12 00:04:32 +0000175{
Nico Huber519be662018-12-23 20:03:35 +0100176 unsigned int i = 0;
Stefan Tauner6ad6e012014-06-12 00:04:32 +0000177 for (; a != 0; a >>= 1)
178 if (a & 1)
179 i++;
180 return i;
181}
182
183int max(int a, int b)
184{
185 return (a > b) ? a : b;
186}
187
188int min(int a, int b)
189{
190 return (a < b) ? a : b;
191}
192
193char *strcat_realloc(char *dest, const char *src)
194{
195 dest = realloc(dest, strlen(dest) + strlen(src) + 1);
196 if (!dest) {
197 msg_gerr("Out of memory!\n");
198 return NULL;
199 }
200 strcat(dest, src);
201 return dest;
202}
203
204void tolower_string(char *str)
205{
206 for (; *str != '\0'; str++)
207 *str = (char)tolower((unsigned char)*str);
208}
209
Marc Schink7ecfe482016-03-17 16:07:23 +0100210uint8_t reverse_byte(uint8_t x)
211{
212 x = ((x >> 1) & 0x55) | ((x << 1) & 0xaa);
213 x = ((x >> 2) & 0x33) | ((x << 2) & 0xcc);
214 x = ((x >> 4) & 0x0f) | ((x << 4) & 0xf0);
215
216 return x;
217}
218
219void reverse_bytes(uint8_t *dst, const uint8_t *src, size_t length)
220{
221 size_t i;
222
223 for (i = 0; i < length; i++)
224 dst[i] = reverse_byte(src[i]);
225}
226
Stefan Taunerb41d8472014-11-01 22:56:06 +0000227/* FIXME: Find a better solution for MinGW. Maybe wrap strtok_s (C11) if it becomes available */
228#ifdef __MINGW32__
229char* strtok_r(char *str, const char *delim, char **nextp)
230{
231 if (str == NULL)
232 str = *nextp;
233
234 str += strspn(str, delim); /* Skip leading delimiters */
235 if (*str == '\0')
236 return NULL;
237
238 char *ret = str;
239 str += strcspn(str, delim); /* Find end of token */
240 if (*str != '\0')
241 *str++ = '\0';
242
243 *nextp = str;
244 return ret;
245}
Miklós Márton8900d6c2019-07-30 00:03:22 +0200246
247/* strndup is a POSIX function not present in MinGW */
248char *strndup(const char *src, size_t maxlen)
249{
Xiang Wang5feb8cd2021-01-20 17:31:19 +0800250 char *retbuf;
251 size_t len;
252 for (len = 0; len < maxlen; len++)
253 if (src[len] == '\0')
254 break;
255 if ((retbuf = malloc(1 + len)) != NULL) {
256 memcpy(retbuf, src, len);
257 retbuf[len] = '\0';
Miklós Márton8900d6c2019-07-30 00:03:22 +0200258 }
Xiang Wang5feb8cd2021-01-20 17:31:19 +0800259 return retbuf;
Miklós Márton8900d6c2019-07-30 00:03:22 +0200260}
Stefan Taunerb41d8472014-11-01 22:56:06 +0000261#endif
262
Stefan Taunerdc627932015-01-27 18:07:50 +0000263/* There is no strnlen in DJGPP */
Nico Huber2d625722016-05-03 10:48:02 +0200264#if defined(__DJGPP__) || (!defined(__LIBPAYLOAD__) && !defined(HAVE_STRNLEN))
Stefan Taunerdc627932015-01-27 18:07:50 +0000265size_t strnlen(const char *str, size_t n)
266{
267 size_t i;
268 for (i = 0; i < n && str[i] != '\0'; i++)
269 ;
270 return i;
271}
272#endif