blob: 5f2b02df8e2d8da8ef5cd471b2bc2a2aadc28968 [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>
21#include "flash.h"
22
Nico Huber7679b5c2023-04-28 21:48:53 +000023int flashprog_read_chunked(struct flashctx *const flash, uint8_t *dst, unsigned int start, unsigned int len,
Nico Huber044c9dc2023-12-29 23:26:57 +010024 unsigned int chunksize, readfunc_t *const read)
Nico Huber7679b5c2023-04-28 21:48:53 +000025{
26 int ret;
27 size_t to_read;
Nico Huber044c9dc2023-12-29 23:26:57 +010028
29 if (chunksize > 256 && chunksize & 3)
30 chunksize &= ~3;
31
Nico Huber7679b5c2023-04-28 21:48:53 +000032 for (; len; len -= to_read, dst += to_read, start += to_read) {
33 to_read = min(chunksize, len);
34 ret = read(flash, dst, start, to_read);
35 if (ret)
36 return ret;
37 flashprog_progress_add(flash, to_read);
38 }
39 return 0;
40}
41
Stefan Tauner6ad6e012014-06-12 00:04:32 +000042/* Returns the minimum number of bits needed to represent the given address.
43 * FIXME: use mind-blowing implementation. */
44uint32_t address_to_bits(uint32_t addr)
45{
46 unsigned int lzb = 0;
Daniel Campello251e3cd2022-03-16 07:05:48 -060047 while (((1u << (31 - lzb)) & ~addr) != 0)
Stefan Tauner6ad6e012014-06-12 00:04:32 +000048 lzb++;
49 return 32 - lzb;
50}
51
Nico Huber519be662018-12-23 20:03:35 +010052unsigned int bitcount(unsigned long a)
Stefan Tauner6ad6e012014-06-12 00:04:32 +000053{
Nico Huber519be662018-12-23 20:03:35 +010054 unsigned int i = 0;
Stefan Tauner6ad6e012014-06-12 00:04:32 +000055 for (; a != 0; a >>= 1)
56 if (a & 1)
57 i++;
58 return i;
59}
60
61int max(int a, int b)
62{
63 return (a > b) ? a : b;
64}
65
66int min(int a, int b)
67{
68 return (a < b) ? a : b;
69}
70
71char *strcat_realloc(char *dest, const char *src)
72{
73 dest = realloc(dest, strlen(dest) + strlen(src) + 1);
74 if (!dest) {
75 msg_gerr("Out of memory!\n");
76 return NULL;
77 }
78 strcat(dest, src);
79 return dest;
80}
81
82void tolower_string(char *str)
83{
84 for (; *str != '\0'; str++)
85 *str = (char)tolower((unsigned char)*str);
86}
87
Marc Schink7ecfe482016-03-17 16:07:23 +010088uint8_t reverse_byte(uint8_t x)
89{
90 x = ((x >> 1) & 0x55) | ((x << 1) & 0xaa);
91 x = ((x >> 2) & 0x33) | ((x << 2) & 0xcc);
92 x = ((x >> 4) & 0x0f) | ((x << 4) & 0xf0);
93
94 return x;
95}
96
97void reverse_bytes(uint8_t *dst, const uint8_t *src, size_t length)
98{
99 size_t i;
100
101 for (i = 0; i < length; i++)
102 dst[i] = reverse_byte(src[i]);
103}
104
Stefan Taunerb41d8472014-11-01 22:56:06 +0000105/* FIXME: Find a better solution for MinGW. Maybe wrap strtok_s (C11) if it becomes available */
106#ifdef __MINGW32__
107char* strtok_r(char *str, const char *delim, char **nextp)
108{
109 if (str == NULL)
110 str = *nextp;
111
112 str += strspn(str, delim); /* Skip leading delimiters */
113 if (*str == '\0')
114 return NULL;
115
116 char *ret = str;
117 str += strcspn(str, delim); /* Find end of token */
118 if (*str != '\0')
119 *str++ = '\0';
120
121 *nextp = str;
122 return ret;
123}
Miklós Márton8900d6c2019-07-30 00:03:22 +0200124
125/* strndup is a POSIX function not present in MinGW */
126char *strndup(const char *src, size_t maxlen)
127{
Xiang Wang5feb8cd2021-01-20 17:31:19 +0800128 char *retbuf;
129 size_t len;
130 for (len = 0; len < maxlen; len++)
131 if (src[len] == '\0')
132 break;
133 if ((retbuf = malloc(1 + len)) != NULL) {
134 memcpy(retbuf, src, len);
135 retbuf[len] = '\0';
Miklós Márton8900d6c2019-07-30 00:03:22 +0200136 }
Xiang Wang5feb8cd2021-01-20 17:31:19 +0800137 return retbuf;
Miklós Márton8900d6c2019-07-30 00:03:22 +0200138}
Stefan Taunerb41d8472014-11-01 22:56:06 +0000139#endif
140
Stefan Taunerdc627932015-01-27 18:07:50 +0000141/* There is no strnlen in DJGPP */
Nico Huber2d625722016-05-03 10:48:02 +0200142#if defined(__DJGPP__) || (!defined(__LIBPAYLOAD__) && !defined(HAVE_STRNLEN))
Stefan Taunerdc627932015-01-27 18:07:50 +0000143size_t strnlen(const char *str, size_t n)
144{
145 size_t i;
146 for (i = 0; i < n && str[i] != '\0'; i++)
147 ;
148 return i;
149}
150#endif