firmware-tools/ptgen: fix minor coding style issues

Signed-off-by: Michael Heimpold <mhei@heimpold.de>
This commit is contained in:
Michael Heimpold 2017-12-21 23:59:28 +01:00 committed by John Crispin
parent 4b275baf91
commit 83f729dfb2

View file

@ -1,4 +1,4 @@
/* /*
* ptgen - partition table generator * ptgen - partition table generator
* Copyright (C) 2006 by Felix Fietkau <nbd@nbd.name> * Copyright (C) 2006 by Felix Fietkau <nbd@nbd.name>
* *
@ -9,12 +9,12 @@
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or * the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
@ -63,25 +63,26 @@ struct partinfo parts[4];
char *filename = NULL; char *filename = NULL;
/* /*
* parse the size argument, which is either * parse the size argument, which is either
* a simple number (K assumed) or * a simple number (K assumed) or
* K, M or G * K, M or G
* *
* returns the size in KByte * returns the size in KByte
*/ */
static long to_kbytes(const char *string) { static long to_kbytes(const char *string)
{
int exp = 0; int exp = 0;
long result; long result;
char *end; char *end;
result = strtoul(string, &end, 0); result = strtoul(string, &end, 0);
switch (tolower(*end)) { switch (tolower(*end)) {
case 'k' : case 'k' :
case '\0' : exp = 0; break; case '\0' : exp = 0; break;
case 'm' : exp = 1; break; case 'm' : exp = 1; break;
case 'g' : exp = 2; break; case 'g' : exp = 2; break;
default: return 0; default: return 0;
} }
if (*end) if (*end)
@ -99,9 +100,10 @@ static long to_kbytes(const char *string) {
} }
/* convert the sector number into a CHS value for the partition table */ /* convert the sector number into a CHS value for the partition table */
static void to_chs(long sect, unsigned char chs[3]) { static void to_chs(long sect, unsigned char chs[3])
{
int c,h,s; int c,h,s;
s = (sect % sectors) + 1; s = (sect % sectors) + 1;
sect = sect / sectors; sect = sect / sectors;
h = sect % heads; h = sect % heads;
@ -116,10 +118,11 @@ static void to_chs(long sect, unsigned char chs[3]) {
} }
/* round the sector number up to the next cylinder */ /* round the sector number up to the next cylinder */
static inline unsigned long round_to_cyl(long sect) { static inline unsigned long round_to_cyl(long sect)
{
int cyl_size = heads * sectors; int cyl_size = heads * sectors;
return sect + cyl_size - (sect % cyl_size); return sect + cyl_size - (sect % cyl_size);
} }
/* round the sector number up to the kb_align boundary */ /* round the sector number up to the kb_align boundary */
@ -131,7 +134,7 @@ static inline unsigned long round_to_kb(long sect) {
static int gen_ptable(uint32_t signature, int nr) static int gen_ptable(uint32_t signature, int nr)
{ {
struct pte pte[4]; struct pte pte[4];
unsigned long sect = 0; unsigned long sect = 0;
int i, fd, ret = -1, start, len; int i, fd, ret = -1, start, len;
memset(pte, 0, sizeof(struct pte) * 4); memset(pte, 0, sizeof(struct pte) * 4);
@ -140,22 +143,27 @@ static int gen_ptable(uint32_t signature, int nr)
fprintf(stderr, "Invalid size in partition %d!\n", i); fprintf(stderr, "Invalid size in partition %d!\n", i);
return -1; return -1;
} }
pte[i].active = ((i + 1) == active) ? 0x80 : 0; pte[i].active = ((i + 1) == active) ? 0x80 : 0;
pte[i].type = parts[i].type; pte[i].type = parts[i].type;
start = sect + sectors; start = sect + sectors;
if (kb_align != 0) if (kb_align != 0)
start = round_to_kb(start); start = round_to_kb(start);
pte[i].start = cpu_to_le32(start); pte[i].start = cpu_to_le32(start);
sect = start + parts[i].size * 2; sect = start + parts[i].size * 2;
if (kb_align == 0) if (kb_align == 0)
sect = round_to_cyl(sect); sect = round_to_cyl(sect);
pte[i].length = cpu_to_le32(len = sect - start); pte[i].length = cpu_to_le32(len = sect - start);
to_chs(start, pte[i].chs_start); to_chs(start, pte[i].chs_start);
to_chs(start + len - 1, pte[i].chs_end); to_chs(start + len - 1, pte[i].chs_end);
if (verbose) if (verbose)
fprintf(stderr, "Partition %d: start=%ld, end=%ld, size=%ld\n", i, (long) start * 512, ((long) start + (long) len) * 512, (long) len * 512); fprintf(stderr, "Partition %d: start=%ld, end=%ld, size=%ld\n", i, (long)start * 512, ((long)start + (long)len) * 512, (long)len * 512);
printf("%ld\n", ((long) start * 512)); printf("%ld\n", (long)start * 512);
printf("%ld\n", ((long) len * 512)); printf("%ld\n", (long)len * 512);
} }
if ((fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0644)) < 0) { if ((fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0644)) < 0) {
@ -179,7 +187,7 @@ static int gen_ptable(uint32_t signature, int nr)
fprintf(stderr, "write failed.\n"); fprintf(stderr, "write failed.\n");
goto fail; goto fail;
} }
ret = 0; ret = 0;
fail: fail:
close(fd); close(fd);
@ -188,7 +196,7 @@ fail:
static void usage(char *prog) static void usage(char *prog)
{ {
fprintf(stderr, "Usage: %s [-v] -h <heads> -s <sectors> -o <outputfile> [-a 0..4] [-l <align kB>] [[-t <type>] -p <size>...] \n", prog); fprintf(stderr, "Usage: %s [-v] -h <heads> -s <sectors> -o <outputfile> [-a 0..4] [-l <align kB>] [[-t <type>] -p <size>...] \n", prog);
exit(1); exit(1);
} }
@ -208,10 +216,10 @@ int main (int argc, char **argv)
verbose++; verbose++;
break; break;
case 'h': case 'h':
heads = (int) strtoul(optarg, NULL, 0); heads = (int)strtoul(optarg, NULL, 0);
break; break;
case 's': case 's':
sectors = (int) strtoul(optarg, NULL, 0); sectors = (int)strtoul(optarg, NULL, 0);
break; break;
case 'p': case 'p':
if (part > 3) { if (part > 3) {
@ -222,15 +230,15 @@ int main (int argc, char **argv)
parts[part++].type = type; parts[part++].type = type;
break; break;
case 't': case 't':
type = (char) strtoul(optarg, NULL, 16); type = (char)strtoul(optarg, NULL, 16);
break; break;
case 'a': case 'a':
active = (int) strtoul(optarg, NULL, 0); active = (int)strtoul(optarg, NULL, 0);
if ((active < 0) || (active > 4)) if ((active < 0) || (active > 4))
active = 0; active = 0;
break; break;
case 'l': case 'l':
kb_align = (int) strtoul(optarg, NULL, 0) * 2; kb_align = (int)strtoul(optarg, NULL, 0) * 2;
break; break;
case 'S': case 'S':
signature = strtoul(optarg, NULL, 0); signature = strtoul(optarg, NULL, 0);
@ -241,7 +249,7 @@ int main (int argc, char **argv)
} }
} }
argc -= optind; argc -= optind;
if (argc || (heads <= 0) || (sectors <= 0) || !filename) if (argc || (heads <= 0) || (sectors <= 0) || !filename)
usage(argv[0]); usage(argv[0]);
return gen_ptable(signature, part); return gen_ptable(signature, part);