| 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 | af9d738 | 2023-05-01 13:33:26 +0200 | [diff] [blame^] | 23 | /* Check if raw data is all 0 or all 1. */ |
| 24 | bool flashprog_no_data(const void *const raw_data, const size_t len) |
| 25 | { |
| 26 | const uint8_t *const raw_end = (const uint8_t *)raw_data + len; |
| 27 | const uint8_t patterns[] = { 0x00, 0xff }; |
| 28 | size_t i; |
| 29 | |
| 30 | for (i = 0; i < ARRAY_SIZE(patterns); ++i) { |
| 31 | const uint8_t *raw_ptr; |
| 32 | for (raw_ptr = raw_data; raw_ptr < raw_end; ++raw_ptr) { |
| 33 | if (*raw_ptr != patterns[i]) |
| 34 | break; |
| 35 | } |
| 36 | if (raw_ptr == raw_end) |
| 37 | return true; |
| 38 | } |
| 39 | |
| 40 | return false; |
| 41 | } |
| 42 | |
| Nico Huber | 7679b5c | 2023-04-28 21:48:53 +0000 | [diff] [blame] | 43 | 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] | 44 | unsigned int chunksize, readfunc_t *const read) |
| Nico Huber | 7679b5c | 2023-04-28 21:48:53 +0000 | [diff] [blame] | 45 | { |
| 46 | int ret; |
| 47 | size_t to_read; |
| Nico Huber | 044c9dc | 2023-12-29 23:26:57 +0100 | [diff] [blame] | 48 | |
| 49 | if (chunksize > 256 && chunksize & 3) |
| 50 | chunksize &= ~3; |
| 51 | |
| Nico Huber | 7679b5c | 2023-04-28 21:48:53 +0000 | [diff] [blame] | 52 | for (; len; len -= to_read, dst += to_read, start += to_read) { |
| 53 | to_read = min(chunksize, len); |
| 54 | ret = read(flash, dst, start, to_read); |
| 55 | if (ret) |
| 56 | return ret; |
| 57 | flashprog_progress_add(flash, to_read); |
| 58 | } |
| 59 | return 0; |
| 60 | } |
| 61 | |
| Stefan Tauner | 6ad6e01 | 2014-06-12 00:04:32 +0000 | [diff] [blame] | 62 | /* Returns the minimum number of bits needed to represent the given address. |
| 63 | * FIXME: use mind-blowing implementation. */ |
| 64 | uint32_t address_to_bits(uint32_t addr) |
| 65 | { |
| 66 | unsigned int lzb = 0; |
| Daniel Campello | 251e3cd | 2022-03-16 07:05:48 -0600 | [diff] [blame] | 67 | while (((1u << (31 - lzb)) & ~addr) != 0) |
| Stefan Tauner | 6ad6e01 | 2014-06-12 00:04:32 +0000 | [diff] [blame] | 68 | lzb++; |
| 69 | return 32 - lzb; |
| 70 | } |
| 71 | |
| Nico Huber | 519be66 | 2018-12-23 20:03:35 +0100 | [diff] [blame] | 72 | unsigned int bitcount(unsigned long a) |
| Stefan Tauner | 6ad6e01 | 2014-06-12 00:04:32 +0000 | [diff] [blame] | 73 | { |
| Nico Huber | 519be66 | 2018-12-23 20:03:35 +0100 | [diff] [blame] | 74 | unsigned int i = 0; |
| Stefan Tauner | 6ad6e01 | 2014-06-12 00:04:32 +0000 | [diff] [blame] | 75 | for (; a != 0; a >>= 1) |
| 76 | if (a & 1) |
| 77 | i++; |
| 78 | return i; |
| 79 | } |
| 80 | |
| 81 | int max(int a, int b) |
| 82 | { |
| 83 | return (a > b) ? a : b; |
| 84 | } |
| 85 | |
| 86 | int min(int a, int b) |
| 87 | { |
| 88 | return (a < b) ? a : b; |
| 89 | } |
| 90 | |
| 91 | char *strcat_realloc(char *dest, const char *src) |
| 92 | { |
| 93 | dest = realloc(dest, strlen(dest) + strlen(src) + 1); |
| 94 | if (!dest) { |
| 95 | msg_gerr("Out of memory!\n"); |
| 96 | return NULL; |
| 97 | } |
| 98 | strcat(dest, src); |
| 99 | return dest; |
| 100 | } |
| 101 | |
| 102 | void tolower_string(char *str) |
| 103 | { |
| 104 | for (; *str != '\0'; str++) |
| 105 | *str = (char)tolower((unsigned char)*str); |
| 106 | } |
| 107 | |
| Marc Schink | 7ecfe48 | 2016-03-17 16:07:23 +0100 | [diff] [blame] | 108 | uint8_t reverse_byte(uint8_t x) |
| 109 | { |
| 110 | x = ((x >> 1) & 0x55) | ((x << 1) & 0xaa); |
| 111 | x = ((x >> 2) & 0x33) | ((x << 2) & 0xcc); |
| 112 | x = ((x >> 4) & 0x0f) | ((x << 4) & 0xf0); |
| 113 | |
| 114 | return x; |
| 115 | } |
| 116 | |
| 117 | void reverse_bytes(uint8_t *dst, const uint8_t *src, size_t length) |
| 118 | { |
| 119 | size_t i; |
| 120 | |
| 121 | for (i = 0; i < length; i++) |
| 122 | dst[i] = reverse_byte(src[i]); |
| 123 | } |
| 124 | |
| Stefan Tauner | b41d847 | 2014-11-01 22:56:06 +0000 | [diff] [blame] | 125 | /* FIXME: Find a better solution for MinGW. Maybe wrap strtok_s (C11) if it becomes available */ |
| 126 | #ifdef __MINGW32__ |
| 127 | char* strtok_r(char *str, const char *delim, char **nextp) |
| 128 | { |
| 129 | if (str == NULL) |
| 130 | str = *nextp; |
| 131 | |
| 132 | str += strspn(str, delim); /* Skip leading delimiters */ |
| 133 | if (*str == '\0') |
| 134 | return NULL; |
| 135 | |
| 136 | char *ret = str; |
| 137 | str += strcspn(str, delim); /* Find end of token */ |
| 138 | if (*str != '\0') |
| 139 | *str++ = '\0'; |
| 140 | |
| 141 | *nextp = str; |
| 142 | return ret; |
| 143 | } |
| Miklós Márton | 8900d6c | 2019-07-30 00:03:22 +0200 | [diff] [blame] | 144 | |
| 145 | /* strndup is a POSIX function not present in MinGW */ |
| 146 | char *strndup(const char *src, size_t maxlen) |
| 147 | { |
| Xiang Wang | 5feb8cd | 2021-01-20 17:31:19 +0800 | [diff] [blame] | 148 | char *retbuf; |
| 149 | size_t len; |
| 150 | for (len = 0; len < maxlen; len++) |
| 151 | if (src[len] == '\0') |
| 152 | break; |
| 153 | if ((retbuf = malloc(1 + len)) != NULL) { |
| 154 | memcpy(retbuf, src, len); |
| 155 | retbuf[len] = '\0'; |
| Miklós Márton | 8900d6c | 2019-07-30 00:03:22 +0200 | [diff] [blame] | 156 | } |
| Xiang Wang | 5feb8cd | 2021-01-20 17:31:19 +0800 | [diff] [blame] | 157 | return retbuf; |
| Miklós Márton | 8900d6c | 2019-07-30 00:03:22 +0200 | [diff] [blame] | 158 | } |
| Stefan Tauner | b41d847 | 2014-11-01 22:56:06 +0000 | [diff] [blame] | 159 | #endif |
| 160 | |
| Stefan Tauner | dc62793 | 2015-01-27 18:07:50 +0000 | [diff] [blame] | 161 | /* There is no strnlen in DJGPP */ |
| Nico Huber | 2d62572 | 2016-05-03 10:48:02 +0200 | [diff] [blame] | 162 | #if defined(__DJGPP__) || (!defined(__LIBPAYLOAD__) && !defined(HAVE_STRNLEN)) |
| Stefan Tauner | dc62793 | 2015-01-27 18:07:50 +0000 | [diff] [blame] | 163 | size_t strnlen(const char *str, size_t n) |
| 164 | { |
| 165 | size_t i; |
| 166 | for (i = 0; i < n && str[i] != '\0'; i++) |
| 167 | ; |
| 168 | return i; |
| 169 | } |
| 170 | #endif |