diff options
author | Stefan Tauner <stefan.tauner@alumni.tuwien.ac.at> | 2014-06-12 00:04:32 +0000 |
---|---|---|
committer | Stefan Tauner <stefan.tauner@alumni.tuwien.ac.at> | 2014-06-12 00:04:32 +0000 |
commit | 6ad6e01e9b961fd5b25cc4d69319a7d29f110684 (patch) | |
tree | 5a7b62f60b10de6a9d9f4f862fdb9f1f8f500f3d | |
parent | a60d408a78be0e0d34dda616977489a307cb98b6 (diff) | |
download | flashrom-6ad6e01e9b961fd5b25cc4d69319a7d29f110684.tar.gz flashrom-6ad6e01e9b961fd5b25cc4d69319a7d29f110684.tar.bz2 flashrom-6ad6e01e9b961fd5b25cc4d69319a7d29f110684.zip |
Introduce helpers.c
Move some suitable functions there, add it to the Makefile, but leave the
declarations in flash.h for now.
Corresponding to flashrom svn r1819.
Signed-off-by: Stefan Tauner <stefan.tauner@alumni.tuwien.ac.at>
Acked-by: Carl-Daniel Hailfinger <c-d.hailfinger.devel.2006@gmx.net>
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | at45db.c | 11 | ||||
-rw-r--r-- | flash.h | 12 | ||||
-rw-r--r-- | flashrom.c | 36 | ||||
-rw-r--r-- | helpers.c | 72 |
5 files changed, 81 insertions, 52 deletions
@@ -353,7 +353,7 @@ CHIP_OBJS = jedec.o stm50.o w39.o w29ee011.o \ ############################################################################### # Library code. -LIB_OBJS = layout.o flashrom.o udelay.o programmer.o +LIB_OBJS = layout.o flashrom.o udelay.o programmer.o helpers.o ############################################################################### # Frontend related stuff. @@ -215,17 +215,6 @@ int probe_spi_at45db(struct flashctx *flash) return 1; } -/* Returns the minimum number of bits needed to represent the given address. - * FIXME: use mind-blowing implementation. - * FIXME: move to utility module. */ -static uint32_t address_to_bits(uint32_t addr) -{ - unsigned int lzb = 0; - while (((1 << (31 - lzb)) & ~addr) != 0) - lzb++; - return 32 - lzb; -} - /* In case of non-power-of-two page sizes we need to convert the address flashrom uses to the address the * DataFlash chips use. The latter uses a segmented address space where the page address is encoded in the * more significant bits and the offset within the page is encoded in the less significant bits. The exact @@ -241,6 +241,14 @@ char *flashbuses_to_text(enum chipbustype bustype); int print_supported(void); void print_supported_wiki(void); +/* helpers.c */ +uint32_t address_to_bits(uint32_t addr); +int bitcount(unsigned long a); +int max(int a, int b); +int min(int a, int b); +char *strcat_realloc(char *dest, const char *src); +void tolower_string(char *str); + /* flashrom.c */ extern int verbose_screen; extern int verbose_logfile; @@ -251,13 +259,9 @@ int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start, uns int erase_flash(struct flashctx *flash); int probe_flash(struct registered_programmer *pgm, int startchip, struct flashctx *fill_flash, int force); int read_flash_to_file(struct flashctx *flash, const char *filename); -int min(int a, int b); -int max(int a, int b); -void tolower_string(char *str); char *extract_param(const char *const *haystack, const char *needle, const char *delim); int verify_range(struct flashctx *flash, const uint8_t *cmpbuf, unsigned int start, unsigned int len); int need_erase(const uint8_t *have, const uint8_t *want, unsigned int len, enum write_granularity gran); -char *strcat_realloc(char *dest, const char *src); void print_version(void); void print_buildinfo(void); void print_banner(void); @@ -535,42 +535,6 @@ int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start, return 0; } -int min(int a, int b) -{ - return (a < b) ? a : b; -} - -int max(int a, int b) -{ - return (a > b) ? a : b; -} - -int bitcount(unsigned long a) -{ - int i = 0; - for (; a != 0; a >>= 1) - if (a & 1) - i++; - return i; -} - -void tolower_string(char *str) -{ - for (; *str != '\0'; str++) - *str = (char)tolower((unsigned char)*str); -} - -char *strcat_realloc(char *dest, const char *src) -{ - dest = realloc(dest, strlen(dest) + strlen(src) + 1); - if (!dest) { - msg_gerr("Out of memory!\n"); - return NULL; - } - strcat(dest, src); - return dest; -} - /* This is a somewhat hacked function similar in some ways to strtok(). * It will look for needle with a subsequent '=' in haystack, return a copy of * needle and remove everything from the first occurrence of needle to the next diff --git a/helpers.c b/helpers.c new file mode 100644 index 00000000..63fc8803 --- /dev/null +++ b/helpers.c @@ -0,0 +1,72 @@ +/* + * This file is part of the flashrom project. + * + * Copyright (C) 2009-2010 Carl-Daniel Hailfinger + * Copyright (C) 2013 Stefan Tauner + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <ctype.h> +#include <stdlib.h> +#include <string.h> +#include "flash.h" + +/* Returns the minimum number of bits needed to represent the given address. + * FIXME: use mind-blowing implementation. */ +uint32_t address_to_bits(uint32_t addr) +{ + unsigned int lzb = 0; + while (((1 << (31 - lzb)) & ~addr) != 0) + lzb++; + return 32 - lzb; +} + +int bitcount(unsigned long a) +{ + int i = 0; + for (; a != 0; a >>= 1) + if (a & 1) + i++; + return i; +} + +int max(int a, int b) +{ + return (a > b) ? a : b; +} + +int min(int a, int b) +{ + return (a < b) ? a : b; +} + +char *strcat_realloc(char *dest, const char *src) +{ + dest = realloc(dest, strlen(dest) + strlen(src) + 1); + if (!dest) { + msg_gerr("Out of memory!\n"); + return NULL; + } + strcat(dest, src); + return dest; +} + +void tolower_string(char *str) +{ + for (; *str != '\0'; str++) + *str = (char)tolower((unsigned char)*str); +} + |