menu: simplify usage for clients
[barebox-mini2440.git] / lib / misc.c
blob549b9601c95a69ea192e744a053d3dee4d996f69
1 /*
2 * misc.c - various assorted functions
4 * Copyright (c) 2007 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
6 * See file CREDITS for list of people who contributed to this
7 * project.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <common.h>
24 #include <malloc.h>
25 #include <errno.h>
26 #include <fs.h>
27 #include <linux/ctype.h>
30 * Like simple_strtoul() but handles an optional G, M, K or k
31 * suffix for Gigabyte, Megabyte or Kilobyte
33 unsigned long strtoul_suffix(const char *str, char **endp, int base)
35 unsigned long val;
36 char *end;
38 val = simple_strtoul(str, &end, base);
40 switch (*end) {
41 case 'G':
42 val *= 1024;
43 case 'M':
44 val *= 1024;
45 case 'k':
46 case 'K':
47 val *= 1024;
48 end++;
49 default:
50 break;
53 if (endp)
54 *endp = (char *)end;
56 return val;
58 EXPORT_SYMBOL(strtoul_suffix);
61 * This function parses strings in the form <startadr>[-endaddr]
62 * or <startadr>[+size] and fills in start and size accordingly.
63 * <startadr> and <endadr> can be given in decimal or hex (with 0x prefix)
64 * and can have an optional G, M, K or k suffix.
66 * examples:
67 * 0x1000-0x2000 -> start = 0x1000, size = 0x1001
68 * 0x1000+0x1000 -> start = 0x1000, size = 0x1000
69 * 0x1000 -> start = 0x1000, size = ~0
70 * 1M+1k -> start = 0x100000, size = 0x400
72 int parse_area_spec(const char *str, ulong *start, ulong *size)
74 char *endp;
75 ulong end;
77 if (!isdigit(*str))
78 return -1;
80 *start = strtoul_suffix(str, &endp, 0);
82 str = endp;
84 if (!*str) {
85 /* beginning given, but no size, assume maximum size */
86 *size = ~0;
87 return 0;
90 if (*str == '-') {
91 /* beginning and end given */
92 end = strtoul_suffix(str + 1, NULL, 0);
93 if (end < *start) {
94 printf("end < start\n");
95 return -1;
97 *size = end - *start + 1;
98 return 0;
101 if (*str == '+') {
102 /* beginning and size given */
103 *size = strtoul_suffix(str + 1, NULL, 0);
104 return 0;
107 return -1;
109 EXPORT_SYMBOL(parse_area_spec);