From 23ee525d3d94d8b4f9469d9519c0af679b2f6773 Mon Sep 17 00:00:00 2001 From: dsl Date: Sat, 2 Jan 2010 21:16:46 +0000 Subject: [PATCH] Change most of the variables that hold disk sector number to be (at least) uint32_t. Might make sysinst work on disks between 1TB and 2TB. Not actually tested because I don't have a big disk. Hopefully I haven't broken the small disk case! Set WARNS= 4 so that comparisons of signed and unsigned block numbers are trapped. --- distrib/utils/sysinst/Makefile.inc | 4 +- distrib/utils/sysinst/defs.h | 6 +- distrib/utils/sysinst/label.c | 94 +++++++++++++++--------------- distrib/utils/sysinst/mbr.c | 42 ++++++------- 4 files changed, 72 insertions(+), 74 deletions(-) diff --git a/distrib/utils/sysinst/Makefile.inc b/distrib/utils/sysinst/Makefile.inc index 8c17f8ef7bbc..c75d5ce43994 100644 --- a/distrib/utils/sysinst/Makefile.inc +++ b/distrib/utils/sysinst/Makefile.inc @@ -1,4 +1,4 @@ -# $NetBSD: Makefile.inc,v 1.48 2009/08/23 20:58:05 jmcneill Exp $ +# $NetBSD: Makefile.inc,v 1.49 2010/01/02 21:16:46 dsl Exp $ # # Makefile for sysinst @@ -7,7 +7,7 @@ COPTS += -Os PROG= sysinst NOMAN= # defined -WARNS= 3 +WARNS= 4 SRCS+= menu_defs.c msg_defs.c main.c install.c upgrade.c \ txtwalk.c run.c factor.c net.c disks.c disks_lfs.c util.c geom.c \ diff --git a/distrib/utils/sysinst/defs.h b/distrib/utils/sysinst/defs.h index 52be53c44ce2..582e38b2093d 100644 --- a/distrib/utils/sysinst/defs.h +++ b/distrib/utils/sysinst/defs.h @@ -1,4 +1,4 @@ -/* $NetBSD: defs.h,v 1.146 2009/10/18 12:09:48 ahoka Exp $ */ +/* $NetBSD: defs.h,v 1.147 2010/01/02 21:16:46 dsl Exp $ */ /* * Copyright 1997 Piermont Information Systems Inc. @@ -153,7 +153,7 @@ enum { /* Round up to the next full cylinder size */ #define NUMSEC(size, sizemult, cylsize) \ - ((size) == -1 ? -1 : (sizemult) == 1 ? (size) : \ + ((size) == ~0u ? ~0u : (sizemult) == 1 ? (size) : \ roundup((size) * (sizemult), (cylsize))) /* What FS type? */ @@ -354,8 +354,6 @@ const char *get_last_mounted(int, int, partinfo *); int savenewlabel(partinfo *, int); int incorelabel(const char *, partinfo *); int edit_and_check_label(partinfo *, int, int, int); -int getpartoff(int); -int getpartsize(int, int); void set_bsize(partinfo *, int); void set_fsize(partinfo *, int); void set_ptype(partinfo *, int, int); diff --git a/distrib/utils/sysinst/label.c b/distrib/utils/sysinst/label.c index dbc05f18e789..5c6529d34998 100644 --- a/distrib/utils/sysinst/label.c +++ b/distrib/utils/sysinst/label.c @@ -1,4 +1,4 @@ -/* $NetBSD: label.c,v 1.53 2009/02/22 11:21:56 ad Exp $ */ +/* $NetBSD: label.c,v 1.54 2010/01/02 21:16:46 dsl Exp $ */ /* * Copyright 1997 Jonathan Stone @@ -36,7 +36,7 @@ #include #if defined(LIBC_SCCS) && !defined(lint) -__RCSID("$NetBSD: label.c,v 1.53 2009/02/22 11:21:56 ad Exp $"); +__RCSID("$NetBSD: label.c,v 1.54 2010/01/02 21:16:46 dsl Exp $"); #endif #include @@ -63,9 +63,11 @@ struct ptn_menu_info { * local prototypes */ static int boringpart(partinfo *, int, int, int); +static uint32_t getpartoff(uint32_t); +static uint32_t getpartsize(uint32_t, uint32_t); static int checklabel(partinfo *, int, int, int, int *, int *); -static void atofsb(const char *, int *, int *); +static int atofsb(const char *, uint32_t *, uint32_t *); /* @@ -102,7 +104,7 @@ checklabel(partinfo *lp, int nparts, int rawpart, int bsdpart, for (i = 0; i < nparts - 1; i ++ ) { partinfo *ip = &lp[i]; - int istart, istop; + uint32_t istart, istop; /* skip unused or reserved partitions */ if (boringpart(lp, i, rawpart, bsdpart)) @@ -117,7 +119,7 @@ checklabel(partinfo *lp, int nparts, int rawpart, int bsdpart, for (j = i+1; j < nparts; j++) { partinfo *jp = &lp[j]; - int jstart, jstop; + uint32_t jstart, jstop; /* skip unused or reserved partitions */ if (boringpart(lp, j, rawpart, bsdpart)) @@ -168,16 +170,16 @@ static int edit_fs_start(menudesc *m, void *arg) { partinfo *p = arg; - int start, size; + uint32_t start, end; start = getpartoff(p->pi_offset); - size = p->pi_size; - if (size != 0) { + if (p->pi_size != 0) { /* Try to keep end in the same place */ - size += p->pi_offset - start; - if (size < 0) - size = 0; - p->pi_size = size; + end = p->pi_offset + p->pi_size; + if (end < start) + p->pi_size = 0; + else + p->pi_size = end - start; } p->pi_offset = start; return 0; @@ -187,10 +189,10 @@ static int edit_fs_size(menudesc *m, void *arg) { partinfo *p = arg; - int size; + uint32_t size; size = getpartsize(p->pi_offset, p->pi_size); - if (size == -1) + if (size == ~0u) size = dlsize - p->pi_offset; p->pi_size = size; return 0; @@ -369,7 +371,7 @@ edit_ptn(menudesc *menu, void *arg) static int fspart_menu = -1; static menu_ent all_fstypes[FSMAXTYPES]; partinfo *p, p_save; - int i; + unsigned int i; if (fspart_menu == -1) { fspart_menu = new_menu(NULL, fs_fields, nelem(fs_fields), @@ -779,11 +781,13 @@ get_last_mounted(int fd, int partstart, partinfo *lp) } /* Ask for a partition offset, check bounds and do the needed roundups */ -int -getpartoff(int defpartstart) +static uint32_t +getpartoff(uint32_t defpartstart) { char defsize[20], isize[20], maxpartc; - int i, localsizemult, partn; + uint32_t i; + uint32_t localsizemult; + int partn; const char *errmsg = "\n"; maxpartc = 'a' + getmaxpartitions() - 1; @@ -803,11 +807,11 @@ getpartoff(int defpartstart) } else if (atoi(isize) == -1) { i = ptstart; localsizemult = 1; - } else - atofsb(isize, &i, &localsizemult); - if (i < 0) { - errmsg = msg_string(MSG_invalid_sector_number); - continue; + } else { + if (atofsb(isize, &i, &localsizemult)) { + errmsg = msg_string(MSG_invalid_sector_number); + continue; + } } /* round to cylinder size if localsizemult != 1 */ i = NUMSEC(i/localsizemult, localsizemult, dlcylsize); @@ -825,13 +829,13 @@ getpartoff(int defpartstart) /* Ask for a partition size, check bounds and do the needed roundups */ -int -getpartsize(int partstart, int defpartsize) +static uint32_t +getpartsize(uint32_t partstart, uint32_t defpartsize) { char dsize[20], isize[20], maxpartc; const char *errmsg = "\n"; - int i, partend, localsizemult; - int fsptend = ptstart + ptsize; + uint32_t i, partend, localsizemult; + uint32_t fsptend = ptstart + ptsize; int partn; maxpartc = 'a' + getmaxpartitions() - 1; @@ -850,11 +854,11 @@ getpartsize(int partstart, int defpartsize) } else if (atoi(isize) == -1) { i = fsptend - partstart; localsizemult = 1; - } else - atofsb(isize, &i, &localsizemult); - if (i < 0) { - errmsg = msg_string(MSG_invalid_sector_number); - continue; + } else { + if (atofsb(isize, &i, &localsizemult)) { + errmsg = msg_string(MSG_invalid_sector_number); + continue; + } } /* * partend is aligned to a cylinder if localsizemult @@ -863,11 +867,11 @@ getpartsize(int partstart, int defpartsize) partend = NUMSEC((partstart + i) / localsizemult, localsizemult, dlcylsize); /* Align to end-of-disk or end-of-slice if close enough */ - i = dlsize - partend; - if (i > -localsizemult && i < localsizemult) + if (partend > (dlsize - localsizemult) + && partend < (dlsize + localsizemult)) partend = dlsize; - i = fsptend - partend; - if (i > -localsizemult && i < localsizemult) + if (partend > (fsptend - localsizemult) + && partend < (fsptend + localsizemult)) partend = fsptend; /* sanity checks */ if (partend > dlsize) { @@ -876,7 +880,6 @@ getpartsize(int partstart, int defpartsize) NULL, isize, 1, (partend - partstart) / sizemult, multname); } - /* return value */ return (partend - partstart); } /* NOTREACHED */ @@ -891,16 +894,15 @@ getpartsize(int partstart, int defpartsize) * returns the number of sectors, and the unit used (for roundups). */ -static void -atofsb(const char *str, int *p_val, int *localsizemult) +static int +atofsb(const char *str, uint32_t *p_val, uint32_t *localsizemult) { int i; - int val; + uint32_t val; *localsizemult = sizemult; if (str[0] == '\0') { - *p_val = -1; - return; + return 1; } val = 0; for (i = 0; str[i] != '\0'; i++) { @@ -910,8 +912,7 @@ atofsb(const char *str, int *p_val, int *localsizemult) } if (str[i + 1] != '\0') { /* A non-digit caracter, not at the end */ - *p_val = -1; - return; + return 1; } if (str[i] == 'G' || str[i] == 'g') { val *= 1024; @@ -931,9 +932,8 @@ atofsb(const char *str, int *p_val, int *localsizemult) break; } /* not a known unit */ - *p_val = -1; - return; + return 1; } *p_val = val * (*localsizemult); - return; + return 0; } diff --git a/distrib/utils/sysinst/mbr.c b/distrib/utils/sysinst/mbr.c index 042006364e35..45a39a7dbe6c 100644 --- a/distrib/utils/sysinst/mbr.c +++ b/distrib/utils/sysinst/mbr.c @@ -1,4 +1,4 @@ -/* $NetBSD: mbr.c,v 1.81 2009/09/19 14:57:27 abs Exp $ */ +/* $NetBSD: mbr.c,v 1.82 2010/01/02 21:16:46 dsl Exp $ */ /* * Copyright 1997 Piermont Information Systems Inc. @@ -114,7 +114,7 @@ struct part_id { }; static int get_mapping(struct mbr_partition *, int, int *, int *, int *, - unsigned long *); + daddr_t *); static void convert_mbr_chs(int, int, int, uint8_t *, uint8_t *, uint8_t *, uint32_t); @@ -252,16 +252,16 @@ disp_cur_geom(void) * in the netbsd disklabel to the part we changed. */ static void -remove_old_partitions(uint start, int size) +remove_old_partitions(uint start, int64_t size) { partinfo *p; uint end; - /* Allow for size being -ve, get it right for very large partitions */ - end = start + size; - if (end < start) { + if (size > 0) { + end = start + size; + } else { end = start; - start = end + size; + start = end - size; } if (end == 0) @@ -275,9 +275,9 @@ remove_old_partitions(uint start, int size) } static int -find_mbr_space(struct mbr_sector *mbrs, uint *start, uint *size, int from, int ignore) +find_mbr_space(struct mbr_sector *mbrs, uint *start, uint *size, uint from, int ignore) { - int sz; + uint sz; int i; uint s, e; @@ -498,7 +498,7 @@ edit_mbr_type(menudesc *m, void *arg) { static menu_ent type_opts[1 + nelem(part_ids)]; static int type_menu = -1; - int i; + unsigned int i; if (type_menu == -1) { for (i = 0; i < nelem(type_opts); i++) { @@ -527,7 +527,7 @@ edit_mbr_start(menudesc *m, void *arg) int opt = mbri->opt; uint start, sz; uint new_r, new, limit, dflt_r; - int delta; + int64_t delta; const char *errmsg; char *cp; struct { @@ -535,10 +535,10 @@ edit_mbr_start(menudesc *m, void *arg) uint start_r; uint limit; } freespace[MBR_PART_COUNT]; - int spaces; - int i; + unsigned int spaces; + unsigned int i; char prompt[MBR_PART_COUNT * 60]; - int len; + unsigned int len; char numbuf[12]; if (opt >= MBR_PART_COUNT) @@ -815,7 +815,7 @@ edit_mbr_size(menudesc *m, void *arg) break; } - if (opt >= MBR_PART_COUNT && max - new <= bsec) + if (opt >= MBR_PART_COUNT && max - new <= (uint32_t)bsec) /* Round up if not enough space for a header for free area */ new = max; @@ -823,7 +823,7 @@ edit_mbr_size(menudesc *m, void *arg) /* Kill information about old partition from label */ mbri->last_mounted[opt < MBR_PART_COUNT ? opt : 0] = NULL; remove_old_partitions(mbri->sector + mbrp->mbrp_start + - mbrp->mbrp_size, new - mbrp->mbrp_size); + mbrp->mbrp_size, (int64_t)new - mbrp->mbrp_size); } mbrp->mbrp_size = new; @@ -1340,7 +1340,7 @@ edit_mbr(mbr_info_t *mbri) } /* Install in only netbsd partition if none tagged */ - if (ptstart == 0 && bsdstart != ~0) { + if (ptstart == 0 && bsdstart != ~0u) { ptstart = bsdstart; ptsize = bsdsize; } @@ -1425,7 +1425,7 @@ read_mbr(const char *disk, mbr_info_t *mbri) for (;;) { if (pread(fd, mbrs, sizeof *mbrs, - (ext_base + next_ext) * (off_t)MBR_SECSIZE) < sizeof *mbrs) + (ext_base + next_ext) * (off_t)MBR_SECSIZE) - sizeof *mbrs != 0) break; if (!valid_mbr(mbrs)) @@ -1706,7 +1706,7 @@ guess_biosgeom_from_mbr(mbr_info_t *mbri, int *cyl, int *head, daddr_t *sec) int xcylinders, xheads, i, j; daddr_t xsectors; int c1, h1, s1, c2, h2, s2; - unsigned long a1, a2; + daddr_t a1, a2; uint64_t num, denom; /* @@ -1791,7 +1791,7 @@ guess_biosgeom_from_mbr(mbr_info_t *mbri, int *cyl, int *head, daddr_t *sec) static int get_mapping(struct mbr_partition *parts, int i, - int *cylinder, int *head, int *sector, unsigned long *absolute) + int *cylinder, int *head, int *sector, daddr_t *absolute) { struct mbr_partition *apart = &parts[i / 2]; @@ -1810,7 +1810,7 @@ get_mapping(struct mbr_partition *parts, int i, + le32toh(apart->mbrp_size) - 1; } /* Sanity check the data against max values */ - if ((((*cylinder * MAXHEAD) + *head) * MAXSECTOR + *sector) < *absolute) + if ((((*cylinder * MAXHEAD) + *head) * (uint32_t)MAXSECTOR + *sector) < *absolute) /* cannot be a CHS mapping */ return -1;