blob: 53fee546ddc4d44f007d3d059cade867d0b016e3 [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 Huberbe42cf22026-02-21 17:40:44 +010023#include "programmer.h"
Stefan Tauner6ad6e012014-06-12 00:04:32 +000024
Nico Huberaf9d7382023-05-01 13:33:26 +020025/* Check if raw data is all 0 or all 1. */
26bool flashprog_no_data(const void *const raw_data, const size_t len)
27{
28 const uint8_t *const raw_end = (const uint8_t *)raw_data + len;
29 const uint8_t patterns[] = { 0x00, 0xff };
30 size_t i;
31
32 for (i = 0; i < ARRAY_SIZE(patterns); ++i) {
33 const uint8_t *raw_ptr;
34 for (raw_ptr = raw_data; raw_ptr < raw_end; ++raw_ptr) {
35 if (*raw_ptr != patterns[i])
36 break;
37 }
38 if (raw_ptr == raw_end)
39 return true;
40 }
41
42 return false;
43}
44
Nico Huber7679b5c2023-04-28 21:48:53 +000045int flashprog_read_chunked(struct flashctx *const flash, uint8_t *dst, unsigned int start, unsigned int len,
Nico Huber044c9dc2023-12-29 23:26:57 +010046 unsigned int chunksize, readfunc_t *const read)
Nico Huber7679b5c2023-04-28 21:48:53 +000047{
48 int ret;
49 size_t to_read;
Nico Huber044c9dc2023-12-29 23:26:57 +010050
51 if (chunksize > 256 && chunksize & 3)
52 chunksize &= ~3;
53
Nico Huber7679b5c2023-04-28 21:48:53 +000054 for (; len; len -= to_read, dst += to_read, start += to_read) {
55 to_read = min(chunksize, len);
56 ret = read(flash, dst, start, to_read);
57 if (ret)
58 return ret;
59 flashprog_progress_add(flash, to_read);
60 }
61 return 0;
62}
63
Nico Huberbe42cf22026-02-21 17:40:44 +010064int flashprog_limit_chip(struct flashctx *flash)
65{
66 const chipsize_t limit = flash->mst.common->max_rom_decode;
67 struct flashchip *const chip = flash->chip;
68 const chipsize_t chip_size = chip->total_size * 1024;
69 unsigned int usable_erasers = 0;
70 unsigned int i;
71
72
73 /* Chip is small enough or already limited. */
74 if (chip_size <= limit)
75 return 0;
76
77 /* Undefine all block_erasers that don't operate on the whole chip,
78 and adjust the eraseblock size of those which do. */
79 for (i = 0; i < NUM_ERASEFUNCTIONS; ++i) {
80 if (chip->block_erasers[i].eraseblocks[0].size != chip_size) {
81 chip->block_erasers[i].eraseblocks[0].count = 0;
82 chip->block_erasers[i].block_erase = NULL;
83 } else {
84 chip->block_erasers[i].eraseblocks[0].size = limit;
85 usable_erasers++;
86 }
87 }
88
89 if (usable_erasers) {
90 chip->total_size = limit / 1024;
91 if (chip->page_size > limit)
92 chip->page_size = limit;
93 return 0;
94 } else {
95 msg_pdbg("Failed to adjust size of chip \"%s\" (%d kB).\n",
96 chip->name, chip->total_size);
97 return -1;
98 }
99}
100
Stefan Tauner6ad6e012014-06-12 00:04:32 +0000101/* Returns the minimum number of bits needed to represent the given address.
102 * FIXME: use mind-blowing implementation. */
103uint32_t address_to_bits(uint32_t addr)
104{
105 unsigned int lzb = 0;
Daniel Campello251e3cd2022-03-16 07:05:48 -0600106 while (((1u << (31 - lzb)) & ~addr) != 0)
Stefan Tauner6ad6e012014-06-12 00:04:32 +0000107 lzb++;
108 return 32 - lzb;
109}
110
Nico Huber519be662018-12-23 20:03:35 +0100111unsigned int bitcount(unsigned long a)
Stefan Tauner6ad6e012014-06-12 00:04:32 +0000112{
Nico Huber519be662018-12-23 20:03:35 +0100113 unsigned int i = 0;
Stefan Tauner6ad6e012014-06-12 00:04:32 +0000114 for (; a != 0; a >>= 1)
115 if (a & 1)
116 i++;
117 return i;
118}
119
120int max(int a, int b)
121{
122 return (a > b) ? a : b;
123}
124
125int min(int a, int b)
126{
127 return (a < b) ? a : b;
128}
129
130char *strcat_realloc(char *dest, const char *src)
131{
132 dest = realloc(dest, strlen(dest) + strlen(src) + 1);
133 if (!dest) {
134 msg_gerr("Out of memory!\n");
135 return NULL;
136 }
137 strcat(dest, src);
138 return dest;
139}
140
141void tolower_string(char *str)
142{
143 for (; *str != '\0'; str++)
144 *str = (char)tolower((unsigned char)*str);
145}
146
Marc Schink7ecfe482016-03-17 16:07:23 +0100147uint8_t reverse_byte(uint8_t x)
148{
149 x = ((x >> 1) & 0x55) | ((x << 1) & 0xaa);
150 x = ((x >> 2) & 0x33) | ((x << 2) & 0xcc);
151 x = ((x >> 4) & 0x0f) | ((x << 4) & 0xf0);
152
153 return x;
154}
155
156void reverse_bytes(uint8_t *dst, const uint8_t *src, size_t length)
157{
158 size_t i;
159
160 for (i = 0; i < length; i++)
161 dst[i] = reverse_byte(src[i]);
162}
163
Stefan Taunerb41d8472014-11-01 22:56:06 +0000164/* FIXME: Find a better solution for MinGW. Maybe wrap strtok_s (C11) if it becomes available */
165#ifdef __MINGW32__
166char* strtok_r(char *str, const char *delim, char **nextp)
167{
168 if (str == NULL)
169 str = *nextp;
170
171 str += strspn(str, delim); /* Skip leading delimiters */
172 if (*str == '\0')
173 return NULL;
174
175 char *ret = str;
176 str += strcspn(str, delim); /* Find end of token */
177 if (*str != '\0')
178 *str++ = '\0';
179
180 *nextp = str;
181 return ret;
182}
Miklós Márton8900d6c2019-07-30 00:03:22 +0200183
184/* strndup is a POSIX function not present in MinGW */
185char *strndup(const char *src, size_t maxlen)
186{
Xiang Wang5feb8cd2021-01-20 17:31:19 +0800187 char *retbuf;
188 size_t len;
189 for (len = 0; len < maxlen; len++)
190 if (src[len] == '\0')
191 break;
192 if ((retbuf = malloc(1 + len)) != NULL) {
193 memcpy(retbuf, src, len);
194 retbuf[len] = '\0';
Miklós Márton8900d6c2019-07-30 00:03:22 +0200195 }
Xiang Wang5feb8cd2021-01-20 17:31:19 +0800196 return retbuf;
Miklós Márton8900d6c2019-07-30 00:03:22 +0200197}
Stefan Taunerb41d8472014-11-01 22:56:06 +0000198#endif
199
Stefan Taunerdc627932015-01-27 18:07:50 +0000200/* There is no strnlen in DJGPP */
Nico Huber2d625722016-05-03 10:48:02 +0200201#if defined(__DJGPP__) || (!defined(__LIBPAYLOAD__) && !defined(HAVE_STRNLEN))
Stefan Taunerdc627932015-01-27 18:07:50 +0000202size_t strnlen(const char *str, size_t n)
203{
204 size_t i;
205 for (i = 0; i < n && str[i] != '\0'; i++)
206 ;
207 return i;
208}
209#endif