Stefan Tauner | 6ad6e01 | 2014-06-12 00:04:32 +0000 | [diff] [blame] | 1 | /* |
| 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 Tauner | 6ad6e01 | 2014-06-12 00:04:32 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include <ctype.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
| 21 | #include "flash.h" |
| 22 | |
Nico Huber | 7679b5c | 2023-04-28 21:48:53 +0000 | [diff] [blame] | 23 | int flashprog_read_chunked(struct flashctx *const flash, uint8_t *dst, unsigned int start, unsigned int len, |
Nico Huber | 044c9dc | 2023-12-29 23:26:57 +0100 | [diff] [blame] | 24 | unsigned int chunksize, readfunc_t *const read) |
Nico Huber | 7679b5c | 2023-04-28 21:48:53 +0000 | [diff] [blame] | 25 | { |
| 26 | int ret; |
| 27 | size_t to_read; |
Nico Huber | 044c9dc | 2023-12-29 23:26:57 +0100 | [diff] [blame] | 28 | |
| 29 | if (chunksize > 256 && chunksize & 3) |
| 30 | chunksize &= ~3; |
| 31 | |
Nico Huber | 7679b5c | 2023-04-28 21:48:53 +0000 | [diff] [blame] | 32 | 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 Tauner | 6ad6e01 | 2014-06-12 00:04:32 +0000 | [diff] [blame] | 42 | /* Returns the minimum number of bits needed to represent the given address. |
| 43 | * FIXME: use mind-blowing implementation. */ |
| 44 | uint32_t address_to_bits(uint32_t addr) |
| 45 | { |
| 46 | unsigned int lzb = 0; |
Daniel Campello | 251e3cd | 2022-03-16 07:05:48 -0600 | [diff] [blame] | 47 | while (((1u << (31 - lzb)) & ~addr) != 0) |
Stefan Tauner | 6ad6e01 | 2014-06-12 00:04:32 +0000 | [diff] [blame] | 48 | lzb++; |
| 49 | return 32 - lzb; |
| 50 | } |
| 51 | |
Nico Huber | 519be66 | 2018-12-23 20:03:35 +0100 | [diff] [blame] | 52 | unsigned int bitcount(unsigned long a) |
Stefan Tauner | 6ad6e01 | 2014-06-12 00:04:32 +0000 | [diff] [blame] | 53 | { |
Nico Huber | 519be66 | 2018-12-23 20:03:35 +0100 | [diff] [blame] | 54 | unsigned int i = 0; |
Stefan Tauner | 6ad6e01 | 2014-06-12 00:04:32 +0000 | [diff] [blame] | 55 | for (; a != 0; a >>= 1) |
| 56 | if (a & 1) |
| 57 | i++; |
| 58 | return i; |
| 59 | } |
| 60 | |
| 61 | int max(int a, int b) |
| 62 | { |
| 63 | return (a > b) ? a : b; |
| 64 | } |
| 65 | |
| 66 | int min(int a, int b) |
| 67 | { |
| 68 | return (a < b) ? a : b; |
| 69 | } |
| 70 | |
| 71 | char *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 | |
| 82 | void tolower_string(char *str) |
| 83 | { |
| 84 | for (; *str != '\0'; str++) |
| 85 | *str = (char)tolower((unsigned char)*str); |
| 86 | } |
| 87 | |
Marc Schink | 7ecfe48 | 2016-03-17 16:07:23 +0100 | [diff] [blame] | 88 | uint8_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 | |
| 97 | void 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 Tauner | b41d847 | 2014-11-01 22:56:06 +0000 | [diff] [blame] | 105 | /* FIXME: Find a better solution for MinGW. Maybe wrap strtok_s (C11) if it becomes available */ |
| 106 | #ifdef __MINGW32__ |
| 107 | char* 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árton | 8900d6c | 2019-07-30 00:03:22 +0200 | [diff] [blame] | 124 | |
| 125 | /* strndup is a POSIX function not present in MinGW */ |
| 126 | char *strndup(const char *src, size_t maxlen) |
| 127 | { |
Xiang Wang | 5feb8cd | 2021-01-20 17:31:19 +0800 | [diff] [blame] | 128 | 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árton | 8900d6c | 2019-07-30 00:03:22 +0200 | [diff] [blame] | 136 | } |
Xiang Wang | 5feb8cd | 2021-01-20 17:31:19 +0800 | [diff] [blame] | 137 | return retbuf; |
Miklós Márton | 8900d6c | 2019-07-30 00:03:22 +0200 | [diff] [blame] | 138 | } |
Stefan Tauner | b41d847 | 2014-11-01 22:56:06 +0000 | [diff] [blame] | 139 | #endif |
| 140 | |
Stefan Tauner | dc62793 | 2015-01-27 18:07:50 +0000 | [diff] [blame] | 141 | /* There is no strnlen in DJGPP */ |
Nico Huber | 2d62572 | 2016-05-03 10:48:02 +0200 | [diff] [blame] | 142 | #if defined(__DJGPP__) || (!defined(__LIBPAYLOAD__) && !defined(HAVE_STRNLEN)) |
Stefan Tauner | dc62793 | 2015-01-27 18:07:50 +0000 | [diff] [blame] | 143 | size_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 |