1*6ed8189eSmrg /* $NetBSD: sunlabel.c,v 1.26 2023/08/11 07:05:39 mrg Exp $ */
203dae382Schristos
303dae382Schristos /*-
403dae382Schristos * Copyright (c) 2002 The NetBSD Foundation, Inc.
503dae382Schristos * All rights reserved.
66659b0b8Smrg *
703dae382Schristos * This code is derived from software contributed to The NetBSD Foundation
89d319abaSmrg * by der Mouse.
96659b0b8Smrg *
1003dae382Schristos * Redistribution and use in source and binary forms, with or without
1103dae382Schristos * modification, are permitted provided that the following conditions
1203dae382Schristos * are met:
1303dae382Schristos * 1. Redistributions of source code must retain the above copyright
1403dae382Schristos * notice, this list of conditions and the following disclaimer.
1503dae382Schristos * 2. Redistributions in binary form must reproduce the above copyright
1603dae382Schristos * notice, this list of conditions and the following disclaimer in the
1703dae382Schristos * documentation and/or other materials provided with the distribution.
186659b0b8Smrg *
1903dae382Schristos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2003dae382Schristos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2103dae382Schristos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2203dae382Schristos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2303dae382Schristos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2403dae382Schristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2503dae382Schristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2603dae382Schristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2703dae382Schristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2803dae382Schristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2903dae382Schristos * POSSIBILITY OF SUCH DAMAGE.
306659b0b8Smrg */
316659b0b8Smrg
3268039cfdSapb #if HAVE_NBTOOL_CONFIG_H
3368039cfdSapb #include "nbtool_config.h"
3468039cfdSapb #endif
3568039cfdSapb
3603dae382Schristos #include <sys/cdefs.h>
37889cc758Saugustss #if defined(__RCSID) && !defined(lint)
38*6ed8189eSmrg __RCSID("$NetBSD: sunlabel.c,v 1.26 2023/08/11 07:05:39 mrg Exp $");
39889cc758Saugustss #endif
406659b0b8Smrg
416659b0b8Smrg #include <stdio.h>
426659b0b8Smrg #include <errno.h>
43d9c0b26cSkleink #include <fcntl.h>
446659b0b8Smrg #include <ctype.h>
456659b0b8Smrg #include <stdlib.h>
466659b0b8Smrg #include <unistd.h>
47f8513999Smatt #ifndef NO_TERMCAP_WIDTH
486659b0b8Smrg #include <termcap.h>
49f8513999Smatt #endif
50889cc758Saugustss #include <string.h>
516659b0b8Smrg #include <strings.h>
5203dae382Schristos #include <inttypes.h>
5303dae382Schristos #include <err.h>
549d319abaSmrg
556659b0b8Smrg #include <sys/ioctl.h>
566659b0b8Smrg
579d319abaSmrg /* If neither S_COMMAND nor NO_S_COMMAND is defined, guess. */
589d319abaSmrg #if !defined(S_COMMAND) && !defined(NO_S_COMMAND)
599d319abaSmrg #define S_COMMAND
609d319abaSmrg #include <util.h>
61889cc758Saugustss #include <sys/disklabel.h>
629d319abaSmrg #endif
639d319abaSmrg
6403dae382Schristos /*
6503dae382Schristos * NPART is the total number of partitions. This must be <= 43, given the
6603dae382Schristos * amount of space available to store extended partitions. It also must be
6703dae382Schristos * <=26, given the use of single letters to name partitions. The 8 is the
6803dae382Schristos * number of `standard' partitions; this arguably should be a #define, since
6903dae382Schristos * it occurs not only here but scattered throughout the code.
7003dae382Schristos */
716659b0b8Smrg #define NPART 16
726659b0b8Smrg #define NXPART (NPART - 8)
736659b0b8Smrg #define PARTLETTER(i) ((i) + 'a')
746659b0b8Smrg #define LETTERPART(i) ((i) - 'a')
756659b0b8Smrg
766659b0b8Smrg /*
7703dae382Schristos * A partition. We keep redundant information around, making sure
7803dae382Schristos * that whenever we change one, we keep another constant and update
7903dae382Schristos * the third. Which one is which depends. Arguably a partition
8003dae382Schristos * should also know its partition number; here, if we need that we
8103dae382Schristos * cheat, using (effectively) ptr-&label.partitions[0].
826659b0b8Smrg */
836659b0b8Smrg struct part {
8403dae382Schristos uint32_t startcyl;
8503dae382Schristos uint32_t nblk;
8603dae382Schristos uint32_t endcyl;
876659b0b8Smrg };
886659b0b8Smrg
896659b0b8Smrg /*
906659b0b8Smrg * A label. As the embedded comments indicate, much of this structure
916659b0b8Smrg * corresponds directly to Sun's struct dk_label. Some of the values
926659b0b8Smrg * here are historical holdovers. Apparently really old Suns did
936659b0b8Smrg * their own sparing in software, so a sector or two per cylinder,
946659b0b8Smrg * plus a whole cylinder or two at the end, got set aside as spares.
956659b0b8Smrg * acyl and apc count those spares, and this is also why ncyl and pcyl
966659b0b8Smrg * both exist. These days the spares generally are hidden from the
976659b0b8Smrg * host by the disk, and there's no reason not to set
986659b0b8Smrg * ncyl=pcyl=ceil(device size/spc) and acyl=apc=0.
996659b0b8Smrg *
1006659b0b8Smrg * Note also that the geometry assumptions behind having nhead and
1016659b0b8Smrg * nsect assume that the sect/trk and trk/cyl values are constant
1026659b0b8Smrg * across the whole drive. The latter is still usually true; the
1036659b0b8Smrg * former isn't. In my experience, you can just put fixed values
1046659b0b8Smrg * here; the basis for software knowing the drive geometry is also
1056659b0b8Smrg * mostly invalid these days anyway. (I just use nhead=32 nsect=64,
1066659b0b8Smrg * which gives me 1M "cylinders", a convenient size.)
1076659b0b8Smrg */
1086659b0b8Smrg struct label {
1096659b0b8Smrg /* BEGIN fields taken directly from struct dk_label */
1106659b0b8Smrg char asciilabel[128];
11103dae382Schristos uint32_t rpm; /* Spindle rotation speed - useless now */
11203dae382Schristos uint32_t pcyl; /* Physical cylinders */
11303dae382Schristos uint32_t apc; /* Alternative sectors per cylinder */
11403dae382Schristos uint32_t obs1; /* Obsolete? */
11503dae382Schristos uint32_t obs2; /* Obsolete? */
11603dae382Schristos uint32_t intrlv; /* Interleave - never anything but 1 IME */
11703dae382Schristos uint32_t ncyl; /* Number of usable cylinders */
11803dae382Schristos uint32_t acyl; /* Alternative cylinders - pcyl minus ncyl */
11903dae382Schristos uint32_t nhead; /* Tracks-per-cylinder (usually # of heads) */
12003dae382Schristos uint32_t nsect; /* Sectors-per-track */
12103dae382Schristos uint32_t obs3; /* Obsolete? */
12203dae382Schristos uint32_t obs4; /* Obsolete? */
1236659b0b8Smrg /* END fields taken directly from struct dk_label */
12403dae382Schristos uint32_t spc; /* Sectors per cylinder - nhead*nsect */
12503dae382Schristos uint32_t dirty:1;/* Modified since last read */
12603dae382Schristos struct part partitions[NPART];/* The partitions themselves */
1276659b0b8Smrg };
1286659b0b8Smrg
1296659b0b8Smrg /*
1306659b0b8Smrg * Describes a field in the label.
1316659b0b8Smrg *
1326659b0b8Smrg * tag is a short name for the field, like "apc" or "nsect". loc is a
1336659b0b8Smrg * pointer to the place in the label where it's stored. print is a
1346659b0b8Smrg * function to print the value; the second argument is the current
1356659b0b8Smrg * column number, and the return value is the new current column
1366659b0b8Smrg * number. (This allows print functions to do proper line wrapping.)
1376659b0b8Smrg * chval is called to change a field; the first argument is the
1386659b0b8Smrg * command line portion that contains the new value (in text form).
1396659b0b8Smrg * The chval function is responsible for parsing and error-checking as
1406659b0b8Smrg * well as doing the modification. changed is a function which does
1416659b0b8Smrg * field-specific actions necessary when the field has been changed.
1426659b0b8Smrg * This could be rolled into the chval function, but I believe this
1436659b0b8Smrg * way provides better code sharing.
1446659b0b8Smrg *
1456659b0b8Smrg * Note that while the fields in the label vary in size (8, 16, or 32
1466659b0b8Smrg * bits), we store everything as ints in the label struct, above, and
1476659b0b8Smrg * convert when packing and unpacking. This allows us to have only
1486659b0b8Smrg * one numeric chval function.
1496659b0b8Smrg */
1506659b0b8Smrg struct field {
1516659b0b8Smrg const char *tag;
1526659b0b8Smrg void *loc;
15303dae382Schristos int (*print)(struct field *, int);
15403dae382Schristos void (*chval)(const char *, struct field *);
1556659b0b8Smrg void (*changed)(void);
1566659b0b8Smrg int taglen;
1576659b0b8Smrg };
1586659b0b8Smrg
1596659b0b8Smrg /* LABEL_MAGIC was chosen by Sun and cannot be trivially changed. */
1606659b0b8Smrg #define LABEL_MAGIC 0xdabe
16103dae382Schristos /*
16203dae382Schristos * LABEL_XMAGIC needs to agree between here and any other code that uses
16303dae382Schristos * extended partitions (mainly the kernel).
16403dae382Schristos */
1656659b0b8Smrg #define LABEL_XMAGIC (0x199d1fe2+8)
1666659b0b8Smrg
1676659b0b8Smrg static int diskfd; /* fd on the disk */
1686659b0b8Smrg static const char *diskname; /* name of the disk, for messages */
1696659b0b8Smrg static int readonly; /* true iff it's open RO */
1706659b0b8Smrg static unsigned char labelbuf[512]; /* Buffer holding the label sector */
17103dae382Schristos static struct label label; /* The label itself. */
17203dae382Schristos static int fixmagic; /* -m, ignore bad magic #s */
17303dae382Schristos static int fixcksum; /* -s, ignore bad cksums */
17403dae382Schristos static int newlabel; /* -n, ignore all on-disk values */
17503dae382Schristos static int quiet; /* -q, don't print chatter */
1766659b0b8Smrg
1776659b0b8Smrg /*
1786659b0b8Smrg * The various functions that go in the field function pointers. The
1796659b0b8Smrg * _ascii functions are for 128-byte string fields (the ASCII label);
1806659b0b8Smrg * the _int functions are for int-valued fields (everything else).
1816659b0b8Smrg * update_spc is a `changed' function for updating the spc value when
1826659b0b8Smrg * changing one of the two values that make it up.
1836659b0b8Smrg */
18403dae382Schristos static int print_ascii(struct field *, int);
18503dae382Schristos static void chval_ascii(const char *, struct field *);
18603dae382Schristos static int print_int(struct field *, int);
18703dae382Schristos static void chval_int(const char *, struct field *);
1886659b0b8Smrg static void update_spc(void);
1896659b0b8Smrg
1906659b0b8Smrg /* The fields themselves. */
19103dae382Schristos static struct field fields[] =
19203dae382Schristos {
1931638ad27Slukem {"ascii", &label.asciilabel[0], print_ascii, chval_ascii, 0, 0 },
1941638ad27Slukem {"rpm", &label.rpm, print_int, chval_int, 0, 0 },
1951638ad27Slukem {"pcyl", &label.pcyl, print_int, chval_int, 0, 0 },
1961638ad27Slukem {"apc", &label.apc, print_int, chval_int, 0, 0 },
1971638ad27Slukem {"obs1", &label.obs1, print_int, chval_int, 0, 0 },
1981638ad27Slukem {"obs2", &label.obs2, print_int, chval_int, 0, 0 },
1991638ad27Slukem {"intrlv", &label.intrlv, print_int, chval_int, 0, 0 },
2001638ad27Slukem {"ncyl", &label.ncyl, print_int, chval_int, 0, 0 },
2011638ad27Slukem {"acyl", &label.acyl, print_int, chval_int, 0, 0 },
2021638ad27Slukem {"nhead", &label.nhead, print_int, chval_int, update_spc, 0 },
2031638ad27Slukem {"nsect", &label.nsect, print_int, chval_int, update_spc, 0 },
2041638ad27Slukem {"obs3", &label.obs3, print_int, chval_int, 0, 0 },
2051638ad27Slukem {"obs4", &label.obs4, print_int, chval_int, 0, 0 },
2061638ad27Slukem {NULL, NULL, NULL, NULL, 0, 0 }
20703dae382Schristos };
208133ea6e5Slukem
2096659b0b8Smrg /*
2106659b0b8Smrg * We'd _like_ to use howmany() from the include files, but can't count
2116659b0b8Smrg * on its being present or working.
2126659b0b8Smrg */
213d030f331Sperry static inline uint32_t how_many(uint32_t amt, uint32_t unit)
214d030f331Sperry __attribute__((const));
215d030f331Sperry static inline uint32_t
how_many(uint32_t amt,uint32_t unit)216133ea6e5Slukem how_many(uint32_t amt, uint32_t unit)
2176659b0b8Smrg {
2186659b0b8Smrg return ((amt + unit - 1) / unit);
2196659b0b8Smrg }
2206659b0b8Smrg
2216659b0b8Smrg /*
2226659b0b8Smrg * Try opening the disk, given a name. If mustsucceed is true, we
2236659b0b8Smrg * "cannot fail"; failures produce gripe-and-exit, and if we return,
2246659b0b8Smrg * our return value is 1. Otherwise, we return 1 on success and 0 on
2256659b0b8Smrg * failure.
2266659b0b8Smrg */
22703dae382Schristos static int
trydisk(const char * s,int mustsucceed)22803dae382Schristos trydisk(const char *s, int mustsucceed)
2296659b0b8Smrg {
23003dae382Schristos int ro = 0;
2316659b0b8Smrg
2326659b0b8Smrg diskname = s;
23303dae382Schristos if ((diskfd = open(s, O_RDWR)) == -1 ||
23488718b25Sapb (diskfd = open(s, O_RDWR | O_NONBLOCK)) == -1) {
23503dae382Schristos if ((diskfd = open(s, O_RDONLY)) == -1) {
23603dae382Schristos if (mustsucceed)
23703dae382Schristos err(1, "Cannot open `%s'", s);
23803dae382Schristos else
23903dae382Schristos return 0;
2406659b0b8Smrg }
2416659b0b8Smrg ro = 1;
2426659b0b8Smrg }
24303dae382Schristos if (ro && !quiet)
24403dae382Schristos warnx("No write access, label is readonly");
2456659b0b8Smrg readonly = ro;
24603dae382Schristos return 1;
2476659b0b8Smrg }
2486659b0b8Smrg
2496659b0b8Smrg /*
2506659b0b8Smrg * Set the disk device, given the user-supplied string. Note that even
2516659b0b8Smrg * if we malloc, we never free, because either trydisk eventually
2526659b0b8Smrg * succeeds, in which case the string is saved in diskname, or it
2536659b0b8Smrg * fails, in which case we exit and freeing is irrelevant.
2546659b0b8Smrg */
25503dae382Schristos static void
setdisk(const char * s)25603dae382Schristos setdisk(const char *s)
2576659b0b8Smrg {
2586659b0b8Smrg char *tmp;
2596659b0b8Smrg
26003dae382Schristos if (strchr(s, '/')) {
26103dae382Schristos trydisk(s, 1);
2626659b0b8Smrg return;
2636659b0b8Smrg }
26403dae382Schristos if (trydisk(s, 0))
26503dae382Schristos return;
26659e06de8Suwe #ifndef DISTRIB /* native tool: search in /dev */
267d9f2774cSitojun asprintf(&tmp, "/dev/%s", s);
268d9f2774cSitojun if (!tmp)
269d9f2774cSitojun err(1, "malloc");
270d9f2774cSitojun if (trydisk(tmp, 0)) {
271d9f2774cSitojun free(tmp);
27203dae382Schristos return;
273d9f2774cSitojun }
274d9f2774cSitojun free(tmp);
275d9f2774cSitojun asprintf(&tmp, "/dev/%s%c", s, getrawpartition() + 'a');
276d9f2774cSitojun if (!tmp)
277d9f2774cSitojun err(1, "malloc");
278d9f2774cSitojun if (trydisk(tmp, 0)) {
279d9f2774cSitojun free(tmp);
28003dae382Schristos return;
281d9f2774cSitojun }
28259e06de8Suwe #endif
28303dae382Schristos errx(1, "Can't find device for disk `%s'", s);
28403dae382Schristos }
28503dae382Schristos
2868b0f9554Sperry static void usage(void) __dead;
28703dae382Schristos static void
usage(void)28803dae382Schristos usage(void)
28903dae382Schristos {
290b635f565Sjmmv (void)fprintf(stderr, "usage: %s [-mnqs] disk\n", getprogname());
2916659b0b8Smrg exit(1);
2926659b0b8Smrg }
2936659b0b8Smrg
2946659b0b8Smrg /*
29503dae382Schristos * Command-line arguments. We can have at most one non-flag
2966659b0b8Smrg * argument, which is the disk name; we can also have flags
2976659b0b8Smrg *
29803dae382Schristos * -m
2996659b0b8Smrg * Turns on fixmagic, which causes bad magic numbers to be
3006659b0b8Smrg * ignored (though a complaint is still printed), rather
3016659b0b8Smrg * than being fatal errors.
3026659b0b8Smrg *
30303dae382Schristos * -s
3046659b0b8Smrg * Turns on fixcksum, which causes bad checksums to be
3056659b0b8Smrg * ignored (though a complaint is still printed), rather
3066659b0b8Smrg * than being fatal errors.
3076659b0b8Smrg *
30803dae382Schristos * -n
3096659b0b8Smrg * Turns on newlabel, which means we're creating a new
3106659b0b8Smrg * label and anything in the label sector should be
31152c78c8aSlukem * ignored. This is a bit like -m -s, except that it
31252c78c8aSlukem * doesn't print complaints and it ignores possible
31352c78c8aSlukem * garbage on-disk.
3146659b0b8Smrg *
3156659b0b8Smrg * -q
3166659b0b8Smrg * Turns on quiet, which suppresses printing of prompts
3176659b0b8Smrg * and other irrelevant chatter. If you're trying to use
3186659b0b8Smrg * sunlabel in an automated way, you probably want this.
3196659b0b8Smrg */
320133ea6e5Slukem static void
handleargs(int ac,char ** av)321133ea6e5Slukem handleargs(int ac, char **av)
3226659b0b8Smrg {
32303dae382Schristos int c;
3246659b0b8Smrg
32552c78c8aSlukem while ((c = getopt(ac, av, "mnqs")) != -1) {
32603dae382Schristos switch (c) {
32703dae382Schristos case 'm':
32803dae382Schristos fixmagic++;
3296659b0b8Smrg break;
33003dae382Schristos case 'n':
33103dae382Schristos newlabel++;
33203dae382Schristos break;
33303dae382Schristos case 'q':
33403dae382Schristos quiet++;
33503dae382Schristos break;
33603dae382Schristos case 's':
33703dae382Schristos fixcksum++;
33803dae382Schristos break;
33903dae382Schristos case '?':
34003dae382Schristos warnx("Illegal option `%c'", c);
34103dae382Schristos usage();
3426659b0b8Smrg }
3436659b0b8Smrg }
34452c78c8aSlukem ac -= optind;
34552c78c8aSlukem av += optind;
34652c78c8aSlukem if (ac != 1)
34752c78c8aSlukem usage();
34852c78c8aSlukem setdisk(av[0]);
34903dae382Schristos }
35052c78c8aSlukem
3516659b0b8Smrg /*
3526659b0b8Smrg * Sets the ending cylinder for a partition. This exists mainly to
3536659b0b8Smrg * centralize the check. (If spc is zero, cylinder numbers make
3546659b0b8Smrg * little sense, and the code would otherwise die on divide-by-0 if we
3556659b0b8Smrg * barged blindly ahead.) We need to call this on a partition
3566659b0b8Smrg * whenever we change it; we need to call it on all partitions
3576659b0b8Smrg * whenever we change spc.
3586659b0b8Smrg */
35903dae382Schristos static void
set_endcyl(struct part * p)36003dae382Schristos set_endcyl(struct part *p)
3616659b0b8Smrg {
36203dae382Schristos if (label.spc == 0) {
36303dae382Schristos p->endcyl = p->startcyl;
36403dae382Schristos } else {
36503dae382Schristos p->endcyl = p->startcyl + how_many(p->nblk, label.spc);
3666659b0b8Smrg }
3676659b0b8Smrg }
3686659b0b8Smrg
3696659b0b8Smrg /*
3706659b0b8Smrg * Unpack a label from disk into the in-core label structure. If
3716659b0b8Smrg * newlabel is set, we don't actually do so; we just synthesize a
3726659b0b8Smrg * blank label instead. This is where knowledge of the Sun label
3736659b0b8Smrg * format is kept for read; pack_label is the corresponding routine
3746659b0b8Smrg * for write. We are careful to use labelbuf, l_s, or l_l as
3756659b0b8Smrg * appropriate to avoid byte-sex issues, so we can work on
3766659b0b8Smrg * little-endian machines.
3776659b0b8Smrg *
3786659b0b8Smrg * Note that a bad magic number for the extended partition information
3796659b0b8Smrg * is not considered an error; it simply indicates there is no
3806659b0b8Smrg * extended partition information. Arguably this is the Wrong Thing,
3816659b0b8Smrg * and we should take zero as meaning no info, and anything other than
3826659b0b8Smrg * zero or LABEL_XMAGIC as reason to gripe.
3836659b0b8Smrg */
38403dae382Schristos static const char *
unpack_label(void)38503dae382Schristos unpack_label(void)
3866659b0b8Smrg {
3876659b0b8Smrg unsigned short int l_s[256];
3886659b0b8Smrg unsigned long int l_l[128];
3896659b0b8Smrg int i;
3906659b0b8Smrg unsigned long int sum;
3916659b0b8Smrg int have_x;
3926659b0b8Smrg
39303dae382Schristos if (newlabel) {
39403dae382Schristos bzero(&label.asciilabel[0], 128);
3956659b0b8Smrg label.rpm = 0;
3966659b0b8Smrg label.pcyl = 0;
3976659b0b8Smrg label.apc = 0;
3986659b0b8Smrg label.obs1 = 0;
3996659b0b8Smrg label.obs2 = 0;
4006659b0b8Smrg label.intrlv = 0;
4016659b0b8Smrg label.ncyl = 0;
4026659b0b8Smrg label.acyl = 0;
4036659b0b8Smrg label.nhead = 0;
4046659b0b8Smrg label.nsect = 0;
4056659b0b8Smrg label.obs3 = 0;
4066659b0b8Smrg label.obs4 = 0;
40703dae382Schristos for (i = 0; i < NPART; i++) {
40803dae382Schristos label.partitions[i].startcyl = 0;
4096659b0b8Smrg label.partitions[i].nblk = 0;
4106659b0b8Smrg set_endcyl(&label.partitions[i]);
4116659b0b8Smrg }
4126659b0b8Smrg label.spc = 0;
4136659b0b8Smrg label.dirty = 1;
4146659b0b8Smrg return (0);
4156659b0b8Smrg }
41603dae382Schristos for (i = 0; i < 256; i++)
41703dae382Schristos l_s[i] = (labelbuf[i + i] << 8) | labelbuf[i + i + 1];
41803dae382Schristos for (i = 0; i < 128; i++)
41903dae382Schristos l_l[i] = (l_s[i + i] << 16) | l_s[i + i + 1];
42003dae382Schristos if (l_s[254] != LABEL_MAGIC) {
42103dae382Schristos if (fixmagic) {
42203dae382Schristos label.dirty = 1;
42303dae382Schristos warnx("ignoring incorrect magic number.");
42403dae382Schristos } else {
42503dae382Schristos return "bad magic number";
4266659b0b8Smrg }
4276659b0b8Smrg }
4286659b0b8Smrg sum = 0;
42903dae382Schristos for (i = 0; i < 256; i++)
43003dae382Schristos sum ^= l_s[i];
4316659b0b8Smrg label.dirty = 0;
43203dae382Schristos if (sum != 0) {
43303dae382Schristos if (fixcksum) {
43403dae382Schristos label.dirty = 1;
43503dae382Schristos warnx("ignoring incorrect checksum.");
43603dae382Schristos } else {
43703dae382Schristos return "checksum wrong";
4386659b0b8Smrg }
4396659b0b8Smrg }
44003dae382Schristos (void)memcpy(&label.asciilabel[0], &labelbuf[0], 128);
4416659b0b8Smrg label.rpm = l_s[210];
4426659b0b8Smrg label.pcyl = l_s[211];
4436659b0b8Smrg label.apc = l_s[212];
4446659b0b8Smrg label.obs1 = l_s[213];
4456659b0b8Smrg label.obs2 = l_s[214];
4466659b0b8Smrg label.intrlv = l_s[215];
4476659b0b8Smrg label.ncyl = l_s[216];
4486659b0b8Smrg label.acyl = l_s[217];
4496659b0b8Smrg label.nhead = l_s[218];
4506659b0b8Smrg label.nsect = l_s[219];
4516659b0b8Smrg label.obs3 = l_s[220];
4526659b0b8Smrg label.obs4 = l_s[221];
4536659b0b8Smrg label.spc = label.nhead * label.nsect;
45403dae382Schristos for (i = 0; i < 8; i++) {
45503dae382Schristos label.partitions[i].startcyl = (uint32_t)l_l[i + i + 111];
45603dae382Schristos label.partitions[i].nblk = (uint32_t)l_l[i + i + 112];
4576659b0b8Smrg set_endcyl(&label.partitions[i]);
4586659b0b8Smrg }
4596659b0b8Smrg have_x = 0;
46003dae382Schristos if (l_l[33] == LABEL_XMAGIC) {
46103dae382Schristos sum = 0;
46203dae382Schristos for (i = 0; i < ((NXPART * 2) + 1); i++)
46303dae382Schristos sum += l_l[33 + i];
46403dae382Schristos if (sum != l_l[32]) {
46503dae382Schristos if (fixcksum) {
46603dae382Schristos label.dirty = 1;
46703dae382Schristos warnx("Ignoring incorrect extended-partition checksum.");
46803dae382Schristos have_x = 1;
46903dae382Schristos } else {
47003dae382Schristos warnx("Extended-partition magic right but checksum wrong.");
47103dae382Schristos }
47203dae382Schristos } else {
4736659b0b8Smrg have_x = 1;
4746659b0b8Smrg }
4756659b0b8Smrg }
47603dae382Schristos if (have_x) {
47703dae382Schristos for (i = 0; i < NXPART; i++) {
47803dae382Schristos int j = i + i + 34;
47903dae382Schristos label.partitions[i + 8].startcyl = (uint32_t)l_l[j++];
48003dae382Schristos label.partitions[i + 8].nblk = (uint32_t)l_l[j++];
4816659b0b8Smrg set_endcyl(&label.partitions[i + 8]);
4826659b0b8Smrg }
48303dae382Schristos } else {
48403dae382Schristos for (i = 0; i < NXPART; i++) {
48503dae382Schristos label.partitions[i + 8].startcyl = 0;
4866659b0b8Smrg label.partitions[i + 8].nblk = 0;
4876659b0b8Smrg set_endcyl(&label.partitions[i + 8]);
4886659b0b8Smrg }
4896659b0b8Smrg }
49003dae382Schristos return 0;
4916659b0b8Smrg }
4926659b0b8Smrg
4936659b0b8Smrg /*
4946659b0b8Smrg * Pack a label from the in-core label structure into on-disk format.
4956659b0b8Smrg * This is where knowledge of the Sun label format is kept for write;
4966659b0b8Smrg * unpack_label is the corresponding routine for read. If all
4976659b0b8Smrg * partitions past the first 8 are size=0 cyl=0, we store all-0s in
4986659b0b8Smrg * the extended partition space, to be fully compatible with Sun
4996659b0b8Smrg * labels. Since AFIAK nothing works in that case that would break if
5006659b0b8Smrg * we put extended partition info there in the same format we'd use if
5016659b0b8Smrg * there were real info there, this is arguably unnecessary, but it's
5026659b0b8Smrg * easy to do.
5036659b0b8Smrg *
5046659b0b8Smrg * We are careful to avoid endianness issues by constructing everything
5056659b0b8Smrg * in an array of shorts. We do this rather than using chars or longs
5066659b0b8Smrg * because the checksum is defined in terms of shorts; using chars or
5076659b0b8Smrg * longs would simplify small amounts of code at the price of
5086659b0b8Smrg * complicating more.
5096659b0b8Smrg */
51003dae382Schristos static void
pack_label(void)51103dae382Schristos pack_label(void)
5126659b0b8Smrg {
5136659b0b8Smrg unsigned short int l_s[256];
5146659b0b8Smrg int i;
5156659b0b8Smrg unsigned short int sum;
5166659b0b8Smrg
51703dae382Schristos memset(&l_s[0], 0, 512);
51803dae382Schristos memcpy(&labelbuf[0], &label.asciilabel[0], 128);
51903dae382Schristos for (i = 0; i < 64; i++)
52003dae382Schristos l_s[i] = (labelbuf[i + i] << 8) | labelbuf[i + i + 1];
5216659b0b8Smrg l_s[210] = label.rpm;
5226659b0b8Smrg l_s[211] = label.pcyl;
5236659b0b8Smrg l_s[212] = label.apc;
5246659b0b8Smrg l_s[213] = label.obs1;
5256659b0b8Smrg l_s[214] = label.obs2;
5266659b0b8Smrg l_s[215] = label.intrlv;
5276659b0b8Smrg l_s[216] = label.ncyl;
5286659b0b8Smrg l_s[217] = label.acyl;
5296659b0b8Smrg l_s[218] = label.nhead;
5306659b0b8Smrg l_s[219] = label.nsect;
5316659b0b8Smrg l_s[220] = label.obs3;
5326659b0b8Smrg l_s[221] = label.obs4;
53303dae382Schristos for (i = 0; i < 8; i++) {
53403dae382Schristos l_s[(i * 4) + 222] = label.partitions[i].startcyl >> 16;
5356659b0b8Smrg l_s[(i * 4) + 223] = label.partitions[i].startcyl & 0xffff;
5366659b0b8Smrg l_s[(i * 4) + 224] = label.partitions[i].nblk >> 16;
5376659b0b8Smrg l_s[(i * 4) + 225] = label.partitions[i].nblk & 0xffff;
5386659b0b8Smrg }
53903dae382Schristos for (i = 0; i < NXPART; i++) {
54003dae382Schristos if (label.partitions[i + 8].startcyl ||
54103dae382Schristos label.partitions[i + 8].nblk)
54203dae382Schristos break;
5436659b0b8Smrg }
54403dae382Schristos if (i < NXPART) {
54503dae382Schristos unsigned long int xsum;
5466659b0b8Smrg l_s[66] = LABEL_XMAGIC >> 16;
5476659b0b8Smrg l_s[67] = LABEL_XMAGIC & 0xffff;
54803dae382Schristos for (i = 0; i < NXPART; i++) {
54903dae382Schristos int j = (i * 4) + 68;
55003dae382Schristos l_s[j++] = label.partitions[i + 8].startcyl >> 16;
55103dae382Schristos l_s[j++] = label.partitions[i + 8].startcyl & 0xffff;
55203dae382Schristos l_s[j++] = label.partitions[i + 8].nblk >> 16;
55303dae382Schristos l_s[j++] = label.partitions[i + 8].nblk & 0xffff;
5546659b0b8Smrg }
5556659b0b8Smrg xsum = 0;
55603dae382Schristos for (i = 0; i < ((NXPART * 2) + 1); i++)
55703dae382Schristos xsum += (l_s[i + i + 66] << 16) | l_s[i + i + 67];
55803dae382Schristos l_s[64] = (int32_t)(xsum >> 16);
55903dae382Schristos l_s[65] = (int32_t)(xsum & 0xffff);
5606659b0b8Smrg }
5616659b0b8Smrg l_s[254] = LABEL_MAGIC;
5626659b0b8Smrg sum = 0;
56303dae382Schristos for (i = 0; i < 255; i++)
56403dae382Schristos sum ^= l_s[i];
5656659b0b8Smrg l_s[255] = sum;
56603dae382Schristos for (i = 0; i < 256; i++) {
56703dae382Schristos labelbuf[i + i] = ((uint32_t)l_s[i]) >> 8;
5686659b0b8Smrg labelbuf[i + i + 1] = l_s[i] & 0xff;
5696659b0b8Smrg }
5706659b0b8Smrg }
5716659b0b8Smrg
5726659b0b8Smrg /*
5736659b0b8Smrg * Get the label. Read it off the disk and unpack it. This function
5746659b0b8Smrg * is nothing but lseek, read, unpack_label, and error checking.
5756659b0b8Smrg */
57603dae382Schristos static void
getlabel(void)57703dae382Schristos getlabel(void)
5786659b0b8Smrg {
5796659b0b8Smrg int rv;
5806659b0b8Smrg const char *lerr;
5816659b0b8Smrg
582e167e82bSjmc if (lseek(diskfd, (off_t)0, SEEK_SET) == (off_t)-1)
58303dae382Schristos err(1, "lseek to 0 on `%s' failed", diskname);
58403dae382Schristos
58503dae382Schristos if ((rv = read(diskfd, &labelbuf[0], 512)) == -1)
58603dae382Schristos err(1, "read label from `%s' failed", diskname);
58703dae382Schristos
5886659b0b8Smrg if (rv != 512)
58903dae382Schristos errx(1, "short read from `%s' wanted %d, got %d.", diskname,
59003dae382Schristos 512, rv);
59103dae382Schristos
5926659b0b8Smrg lerr = unpack_label();
5936659b0b8Smrg if (lerr)
594eda9e509Sgrant errx(1, "bogus label on `%s' (%s)", diskname, lerr);
5956659b0b8Smrg }
5966659b0b8Smrg
5976659b0b8Smrg /*
5986659b0b8Smrg * Put the label. Pack it and write it to the disk. This function is
5996659b0b8Smrg * little more than pack_label, lseek, write, and error checking.
6006659b0b8Smrg */
60103dae382Schristos static void
putlabel(void)60203dae382Schristos putlabel(void)
6036659b0b8Smrg {
6046659b0b8Smrg int rv;
6056659b0b8Smrg
60603dae382Schristos if (readonly) {
60703dae382Schristos warnx("No write access to `%s'", diskname);
6086659b0b8Smrg return;
6096659b0b8Smrg }
61003dae382Schristos
611e167e82bSjmc if (lseek(diskfd, (off_t)0, SEEK_SET) < (off_t)-1)
61203dae382Schristos err(1, "lseek to 0 on `%s' failed", diskname);
61303dae382Schristos
6146659b0b8Smrg pack_label();
61503dae382Schristos
61603dae382Schristos if ((rv = write(diskfd, &labelbuf[0], 512)) == -1) {
61703dae382Schristos err(1, "write label to `%s' failed", diskname);
6186659b0b8Smrg exit(1);
6196659b0b8Smrg }
62003dae382Schristos
6216659b0b8Smrg if (rv != 512)
62203dae382Schristos errx(1, "short write to `%s': wanted %d, got %d",
62303dae382Schristos diskname, 512, rv);
62403dae382Schristos
6256659b0b8Smrg label.dirty = 0;
6266659b0b8Smrg }
6276659b0b8Smrg
6286659b0b8Smrg /*
6296659b0b8Smrg * Skip whitespace. Used several places in the command-line parsing
6306659b0b8Smrg * code.
6316659b0b8Smrg */
63203dae382Schristos static void
skipspaces(const char ** cpp)63303dae382Schristos skipspaces(const char **cpp)
6346659b0b8Smrg {
63503dae382Schristos const char *cp = *cpp;
63603dae382Schristos while (*cp && isspace((unsigned char)*cp))
63703dae382Schristos cp++;
63803dae382Schristos *cpp = cp;
6396659b0b8Smrg }
6406659b0b8Smrg
6416659b0b8Smrg /*
6426659b0b8Smrg * Scan a number. The first arg points to the char * that's moving
6436659b0b8Smrg * along the string. The second arg points to where we should store
6446659b0b8Smrg * the result. The third arg says what we're scanning, for errors.
6456659b0b8Smrg * The return value is 0 on error, or nonzero if all goes well.
6466659b0b8Smrg */
64703dae382Schristos static int
scannum(const char ** cpp,uint32_t * np,const char * tag)64803dae382Schristos scannum(const char **cpp, uint32_t *np, const char *tag)
6496659b0b8Smrg {
65003dae382Schristos uint32_t v;
6516659b0b8Smrg int nd;
65203dae382Schristos const char *cp;
6536659b0b8Smrg
6546659b0b8Smrg skipspaces(cpp);
6556659b0b8Smrg v = 0;
6566659b0b8Smrg nd = 0;
65703dae382Schristos
65803dae382Schristos cp = *cpp;
6599122339bSdsl while (*cp && isdigit((unsigned char)*cp)) {
66003dae382Schristos v = (10 * v) + (*cp++ - '0');
6616659b0b8Smrg nd++;
6626659b0b8Smrg }
66303dae382Schristos *cpp = cp;
66403dae382Schristos
66503dae382Schristos if (nd == 0) {
66603dae382Schristos printf("Missing/invalid %s: %s\n", tag, cp);
6676659b0b8Smrg return (0);
6686659b0b8Smrg }
6696659b0b8Smrg *np = v;
6706659b0b8Smrg return (1);
6716659b0b8Smrg }
6726659b0b8Smrg
6736659b0b8Smrg /*
6746659b0b8Smrg * Change a partition. pno is the number of the partition to change;
6756659b0b8Smrg * numbers is a pointer to the string containing the specification for
6766659b0b8Smrg * the new start and size. This always takes the form "start size",
6776659b0b8Smrg * where start can be
6786659b0b8Smrg *
6796659b0b8Smrg * a number
6806659b0b8Smrg * The partition starts at the beginning of that cylinder.
6816659b0b8Smrg *
6826659b0b8Smrg * start-X
6836659b0b8Smrg * The partition starts at the same place partition X does.
6846659b0b8Smrg *
6856659b0b8Smrg * end-X
6866659b0b8Smrg * The partition starts at the place partition X ends. If
6876659b0b8Smrg * partition X does not exactly on a cylinder boundary, it
6886659b0b8Smrg * is effectively rounded up.
6896659b0b8Smrg *
6906659b0b8Smrg * and size can be
6916659b0b8Smrg *
6926659b0b8Smrg * a number
6936659b0b8Smrg * The partition is that many sectors long.
6946659b0b8Smrg *
6956659b0b8Smrg * num/num/num
6966659b0b8Smrg * The three numbers are cyl/trk/sect counts. n1/n2/n3 is
6976659b0b8Smrg * equivalent to specifying a single number
6986659b0b8Smrg * ((n1*label.nhead)+n2)*label.nsect)+n3. In particular,
6996659b0b8Smrg * if label.nhead or label.nsect is zero, this has limited
7006659b0b8Smrg * usefulness.
7016659b0b8Smrg *
7026659b0b8Smrg * end-X
7036659b0b8Smrg * The partition ends where partition X ends. It is an
7046659b0b8Smrg * error for partition X to end before the specified start
7056659b0b8Smrg * point. This always goes to exactly where partition X
7066659b0b8Smrg * ends, even if that's partway through a cylinder.
7076659b0b8Smrg *
7086659b0b8Smrg * start-X
7096659b0b8Smrg * The partition extends to end exactly where partition X
7106659b0b8Smrg * begins. It is an error for partition X to begin before
7116659b0b8Smrg * the specified start point.
7126659b0b8Smrg *
7136659b0b8Smrg * size-X
7146659b0b8Smrg * The partition has the same size as partition X.
7156659b0b8Smrg *
7166659b0b8Smrg * If label.spc is nonzero but the partition size is not a multiple of
7176659b0b8Smrg * it, a warning is printed, since you usually don't want this. Most
7186659b0b8Smrg * often, in my experience, this comes from specifying a cylinder
7196659b0b8Smrg * count as a single number N instead of N/0/0.
7206659b0b8Smrg */
72103dae382Schristos static void
chpart(int pno,const char * numbers)72203dae382Schristos chpart(int pno, const char *numbers)
7236659b0b8Smrg {
72403dae382Schristos uint32_t cyl0;
72503dae382Schristos uint32_t size;
72603dae382Schristos uint32_t sizec;
72703dae382Schristos uint32_t sizet;
72803dae382Schristos uint32_t sizes;
7296659b0b8Smrg
7306659b0b8Smrg skipspaces(&numbers);
73103dae382Schristos if (!memcmp(numbers, "end-", 4) && numbers[4]) {
73203dae382Schristos int epno = LETTERPART(numbers[4]);
73303dae382Schristos if ((epno >= 0) && (epno < NPART)) {
73403dae382Schristos cyl0 = label.partitions[epno].endcyl;
7356659b0b8Smrg numbers += 5;
73603dae382Schristos } else {
73703dae382Schristos if (!scannum(&numbers, &cyl0, "starting cylinder"))
73803dae382Schristos return;
7396659b0b8Smrg }
74003dae382Schristos } else if (!memcmp(numbers, "start-", 6) && numbers[6]) {
74103dae382Schristos int spno = LETTERPART(numbers[6]);
74203dae382Schristos if ((spno >= 0) && (spno < NPART)) {
74303dae382Schristos cyl0 = label.partitions[spno].startcyl;
7446659b0b8Smrg numbers += 7;
74503dae382Schristos } else {
74603dae382Schristos if (!scannum(&numbers, &cyl0, "starting cylinder"))
74703dae382Schristos return;
7486659b0b8Smrg }
74903dae382Schristos } else {
75003dae382Schristos if (!scannum(&numbers, &cyl0, "starting cylinder"))
75103dae382Schristos return;
7526659b0b8Smrg }
7536659b0b8Smrg skipspaces(&numbers);
75403dae382Schristos if (!memcmp(numbers, "end-", 4) && numbers[4]) {
75503dae382Schristos int epno = LETTERPART(numbers[4]);
75603dae382Schristos if ((epno >= 0) && (epno < NPART)) {
75703dae382Schristos if (label.partitions[epno].endcyl <= cyl0) {
75803dae382Schristos warnx("Partition %c ends before cylinder %u",
75903dae382Schristos PARTLETTER(epno), cyl0);
7606659b0b8Smrg return;
7616659b0b8Smrg }
7626659b0b8Smrg size = label.partitions[epno].nblk;
7636659b0b8Smrg /* Be careful of unsigned arithmetic */
76403dae382Schristos if (cyl0 > label.partitions[epno].startcyl) {
76503dae382Schristos size -= (cyl0 - label.partitions[epno].startcyl)
76603dae382Schristos * label.spc;
76703dae382Schristos } else if (cyl0 < label.partitions[epno].startcyl) {
76803dae382Schristos size += (label.partitions[epno].startcyl - cyl0)
76903dae382Schristos * label.spc;
7706659b0b8Smrg }
7716659b0b8Smrg numbers += 5;
77203dae382Schristos } else {
77303dae382Schristos if (!scannum(&numbers, &size, "partition size"))
7746659b0b8Smrg return;
7756659b0b8Smrg }
77603dae382Schristos } else if (!memcmp(numbers, "start-", 6) && numbers[6]) {
77703dae382Schristos int spno = LETTERPART(numbers[6]);
77803dae382Schristos if ((spno >= 0) && (spno < NPART)) {
77903dae382Schristos if (label.partitions[spno].startcyl <= cyl0) {
78003dae382Schristos warnx("Partition %c starts before cylinder %u",
78103dae382Schristos PARTLETTER(spno), cyl0);
78203dae382Schristos return;
78303dae382Schristos }
78403dae382Schristos size = (label.partitions[spno].startcyl - cyl0)
78503dae382Schristos * label.spc;
7866659b0b8Smrg numbers += 7;
78703dae382Schristos } else {
78803dae382Schristos if (!scannum(&numbers, &size, "partition size"))
78903dae382Schristos return;
7906659b0b8Smrg }
79103dae382Schristos } else if (!memcmp(numbers, "size-", 5) && numbers[5]) {
79203dae382Schristos int spno = LETTERPART(numbers[5]);
79303dae382Schristos if ((spno >= 0) && (spno < NPART)) {
79403dae382Schristos size = label.partitions[spno].nblk;
7956659b0b8Smrg numbers += 6;
79603dae382Schristos } else {
79703dae382Schristos if (!scannum(&numbers, &size, "partition size"))
79803dae382Schristos return;
7996659b0b8Smrg }
80003dae382Schristos } else {
80103dae382Schristos if (!scannum(&numbers, &size, "partition size"))
80203dae382Schristos return;
8036659b0b8Smrg skipspaces(&numbers);
80403dae382Schristos if (*numbers == '/') {
80503dae382Schristos sizec = size;
8066659b0b8Smrg numbers++;
80703dae382Schristos if (!scannum(&numbers, &sizet,
80803dae382Schristos "partition size track value"))
80903dae382Schristos return;
8106659b0b8Smrg skipspaces(&numbers);
81103dae382Schristos if (*numbers != '/') {
81203dae382Schristos warnx("Invalid c/t/s syntax - no second slash");
8136659b0b8Smrg return;
8146659b0b8Smrg }
8156659b0b8Smrg numbers++;
81603dae382Schristos if (!scannum(&numbers, &sizes,
81703dae382Schristos "partition size sector value"))
81803dae382Schristos return;
81903dae382Schristos size = sizes + (label.nsect * (sizet
82003dae382Schristos + (label.nhead * sizec)));
8216659b0b8Smrg }
8226659b0b8Smrg }
82303dae382Schristos if (label.spc && (size % label.spc)) {
824eda9e509Sgrant warnx("Size is not a multiple of cylinder size (is %u/%u/%u)",
82503dae382Schristos size / label.spc,
82603dae382Schristos (size % label.spc) / label.nsect, size % label.nsect);
8276659b0b8Smrg }
8286659b0b8Smrg label.partitions[pno].startcyl = cyl0;
8296659b0b8Smrg label.partitions[pno].nblk = size;
8306659b0b8Smrg set_endcyl(&label.partitions[pno]);
83103dae382Schristos if ((label.partitions[pno].startcyl * label.spc)
83203dae382Schristos + label.partitions[pno].nblk > label.spc * label.ncyl) {
83303dae382Schristos warnx("Partition extends beyond end of disk");
8346659b0b8Smrg }
8356659b0b8Smrg label.dirty = 1;
8366659b0b8Smrg }
8376659b0b8Smrg
8386659b0b8Smrg /*
8396659b0b8Smrg * Change a 128-byte-string field. There's currently only one such,
8406659b0b8Smrg * the ASCII label field.
8416659b0b8Smrg */
84203dae382Schristos static void
chval_ascii(const char * cp,struct field * f)84303dae382Schristos chval_ascii(const char *cp, struct field *f)
8446659b0b8Smrg {
8456659b0b8Smrg const char *nl;
8466659b0b8Smrg
8476659b0b8Smrg skipspaces(&cp);
84803dae382Schristos if ((nl = strchr(cp, '\n')) == NULL)
84903dae382Schristos nl = cp + strlen(cp);
85003dae382Schristos if (nl - cp > 128) {
85103dae382Schristos warnx("Ascii label string too long - max 128 characters");
85203dae382Schristos } else {
85303dae382Schristos memset(f->loc, 0, 128);
85403dae382Schristos memcpy(f->loc, cp, (size_t)(nl - cp));
8556659b0b8Smrg label.dirty = 1;
8566659b0b8Smrg }
8576659b0b8Smrg }
8586659b0b8Smrg /*
8596659b0b8Smrg * Change an int-valued field. As noted above, there's only one
8606659b0b8Smrg * function, regardless of the field size in the on-disk label.
8616659b0b8Smrg */
86203dae382Schristos static void
chval_int(const char * cp,struct field * f)86303dae382Schristos chval_int(const char *cp, struct field *f)
8646659b0b8Smrg {
86503dae382Schristos uint32_t v;
8666659b0b8Smrg
86703dae382Schristos if (!scannum(&cp, &v, "value"))
86803dae382Schristos return;
86903dae382Schristos *(uint32_t *)f->loc = v;
8706659b0b8Smrg label.dirty = 1;
8716659b0b8Smrg }
8726659b0b8Smrg /*
8736659b0b8Smrg * Change a field's value. The string argument contains the field name
8746659b0b8Smrg * and the new value in text form. Look up the field and call its
8756659b0b8Smrg * chval and changed functions.
8766659b0b8Smrg */
87703dae382Schristos static void
chvalue(const char * str)87803dae382Schristos chvalue(const char *str)
8796659b0b8Smrg {
8806659b0b8Smrg const char *cp;
8816659b0b8Smrg int i;
88203dae382Schristos size_t n;
8836659b0b8Smrg
88403dae382Schristos if (fields[0].taglen < 1) {
88503dae382Schristos for (i = 0; fields[i].tag; i++)
88603dae382Schristos fields[i].taglen = strlen(fields[i].tag);
8876659b0b8Smrg }
8886659b0b8Smrg skipspaces(&str);
8896659b0b8Smrg cp = str;
8909122339bSdsl while (*cp && !isspace((unsigned char)*cp))
89103dae382Schristos cp++;
8926659b0b8Smrg n = cp - str;
89303dae382Schristos for (i = 0; fields[i].tag; i++) {
8941638ad27Slukem if (((int)n == fields[i].taglen) && !memcmp(str, fields[i].tag, n)) {
89503dae382Schristos (*fields[i].chval) (cp, &fields[i]);
89603dae382Schristos if (fields[i].changed)
89703dae382Schristos (*fields[i].changed)();
8986659b0b8Smrg break;
8996659b0b8Smrg }
9006659b0b8Smrg }
9016659b0b8Smrg if (!fields[i].tag)
90252c78c8aSlukem warnx("Bad name %.*s - see L output for names", (int)n, str);
9036659b0b8Smrg }
9046659b0b8Smrg
9056659b0b8Smrg /*
9066659b0b8Smrg * `changed' function for the ntrack and nsect fields; update label.spc
9076659b0b8Smrg * and call set_endcyl on all partitions.
9086659b0b8Smrg */
90903dae382Schristos static void
update_spc(void)91003dae382Schristos update_spc(void)
9116659b0b8Smrg {
9126659b0b8Smrg int i;
9136659b0b8Smrg
9146659b0b8Smrg label.spc = label.nhead * label.nsect;
91503dae382Schristos for (i = 0; i < NPART; i++)
91603dae382Schristos set_endcyl(&label.partitions[i]);
9176659b0b8Smrg }
9186659b0b8Smrg
9196659b0b8Smrg /*
9206659b0b8Smrg * Print function for 128-byte-string fields. Currently only the ASCII
9216659b0b8Smrg * label, but we don't depend on that.
9226659b0b8Smrg */
92303dae382Schristos static int
print_ascii(struct field * f,int sofar)924881509a4Sdogcow print_ascii(struct field *f, int sofar)
9256659b0b8Smrg {
9266659b0b8Smrg printf("%s: %.128s\n", f->tag, (char *)f->loc);
92703dae382Schristos return 0;
9286659b0b8Smrg }
9296659b0b8Smrg
9306659b0b8Smrg /*
9316659b0b8Smrg * Print an int-valued field. We are careful to do proper line wrap,
9326659b0b8Smrg * making each value occupy 16 columns.
9336659b0b8Smrg */
93403dae382Schristos static int
print_int(struct field * f,int sofar)93503dae382Schristos print_int(struct field *f, int sofar)
9366659b0b8Smrg {
93703dae382Schristos if (sofar >= 60) {
93803dae382Schristos printf("\n");
9396659b0b8Smrg sofar = 0;
9406659b0b8Smrg }
94103dae382Schristos printf("%s: %-*u", f->tag, 14 - (int)strlen(f->tag),
94203dae382Schristos *(uint32_t *)f->loc);
94303dae382Schristos return sofar + 16;
9446659b0b8Smrg }
9456659b0b8Smrg
9466659b0b8Smrg /*
9476659b0b8Smrg * Print the whole label. Just call the print function for each field,
9486659b0b8Smrg * then append a newline if necessary.
9496659b0b8Smrg */
95003dae382Schristos static void
print_label(void)95103dae382Schristos print_label(void)
9526659b0b8Smrg {
9536659b0b8Smrg int i;
9546659b0b8Smrg int c;
9556659b0b8Smrg
9566659b0b8Smrg c = 0;
95703dae382Schristos for (i = 0; fields[i].tag; i++)
95803dae382Schristos c = (*fields[i].print) (&fields[i], c);
95903dae382Schristos if (c > 0)
96003dae382Schristos printf("\n");
9616659b0b8Smrg }
9626659b0b8Smrg
9636659b0b8Smrg /*
9646659b0b8Smrg * Figure out how many columns wide the screen is. We impose a minimum
9656659b0b8Smrg * width of 20 columns; I suspect the output code has some issues if
9666659b0b8Smrg * we have fewer columns than partitions.
9676659b0b8Smrg */
96803dae382Schristos static int
screen_columns(void)96903dae382Schristos screen_columns(void)
9706659b0b8Smrg {
9716659b0b8Smrg int ncols;
9726659b0b8Smrg #ifndef NO_TERMCAP_WIDTH
9736659b0b8Smrg char *term;
9746659b0b8Smrg char tbuf[1024];
9756659b0b8Smrg #endif
9766659b0b8Smrg #if defined(TIOCGWINSZ)
9776659b0b8Smrg struct winsize wsz;
9786659b0b8Smrg #elif defined(TIOCGSIZE)
9796659b0b8Smrg struct ttysize tsz;
9806659b0b8Smrg #endif
9816659b0b8Smrg
9826659b0b8Smrg ncols = 80;
9836659b0b8Smrg #ifndef NO_TERMCAP_WIDTH
9846659b0b8Smrg term = getenv("TERM");
98503dae382Schristos if (term && (tgetent(&tbuf[0], term) == 1)) {
98603dae382Schristos int n = tgetnum("co");
98703dae382Schristos if (n > 1)
98803dae382Schristos ncols = n;
9896659b0b8Smrg }
9906659b0b8Smrg #endif
9916659b0b8Smrg #if defined(TIOCGWINSZ)
99203dae382Schristos if ((ioctl(1, TIOCGWINSZ, &wsz) == 0) && (wsz.ws_col > 0)) {
99303dae382Schristos ncols = wsz.ws_col;
9946659b0b8Smrg }
9956659b0b8Smrg #elif defined(TIOCGSIZE)
99603dae382Schristos if ((ioctl(1, TIOCGSIZE, &tsz) == 0) && (tsz.ts_cols > 0)) {
99703dae382Schristos ncols = tsz.ts_cols;
9986659b0b8Smrg }
9996659b0b8Smrg #endif
100003dae382Schristos if (ncols < 20)
100103dae382Schristos ncols = 20;
100203dae382Schristos return ncols;
10036659b0b8Smrg }
10046659b0b8Smrg
10056659b0b8Smrg /*
10066659b0b8Smrg * Print the partitions. The argument is true iff we should print all
10076659b0b8Smrg * partitions, even those set start=0 size=0. We generate one line
10086659b0b8Smrg * per partition (or, if all==0, per `interesting' partition), plus a
10096659b0b8Smrg * visually graphic map of partition letters. Most of the hair in the
10106659b0b8Smrg * visual display lies in ensuring that nothing takes up less than one
10116659b0b8Smrg * character column, that if two boundaries appear visually identical,
10126659b0b8Smrg * they _are_ identical. Within that constraint, we try to make the
10136659b0b8Smrg * number of character columns proportional to the size....
10146659b0b8Smrg */
101503dae382Schristos static void
print_part(int all)101603dae382Schristos print_part(int all)
10176659b0b8Smrg {
101803dae382Schristos int i, j, k, n, r, c;
101903dae382Schristos size_t ncols;
102003dae382Schristos uint32_t edges[2 * NPART];
1021*6ed8189eSmrg int ce[2 * NPART] = {0}; /* XXXGCC12 */
10226659b0b8Smrg int row[NPART];
10236659b0b8Smrg unsigned char table[2 * NPART][NPART];
10246659b0b8Smrg char *line;
102503dae382Schristos struct part *p = label.partitions;
10266659b0b8Smrg
102703dae382Schristos for (i = 0; i < NPART; i++) {
102803dae382Schristos if (all || p[i].startcyl || p[i].nblk) {
102903dae382Schristos printf("%c: start cyl = %6u, size = %8u (",
103003dae382Schristos PARTLETTER(i), p[i].startcyl, p[i].nblk);
103103dae382Schristos if (label.spc) {
103203dae382Schristos printf("%u/%u/%u - ", p[i].nblk / label.spc,
10336659b0b8Smrg (p[i].nblk % label.spc) / label.nsect,
10346659b0b8Smrg p[i].nblk % label.nsect);
10356659b0b8Smrg }
10366659b0b8Smrg printf("%gMb)\n", p[i].nblk / 2048.0);
10376659b0b8Smrg }
10386659b0b8Smrg }
103903dae382Schristos
10406659b0b8Smrg j = 0;
104103dae382Schristos for (i = 0; i < NPART; i++) {
104203dae382Schristos if (p[i].nblk > 0) {
104303dae382Schristos edges[j++] = p[i].startcyl;
10446659b0b8Smrg edges[j++] = p[i].endcyl;
10456659b0b8Smrg }
10466659b0b8Smrg }
104703dae382Schristos
104803dae382Schristos do {
104903dae382Schristos n = 0;
105003dae382Schristos for (i = 1; i < j; i++) {
105103dae382Schristos if (edges[i] < edges[i - 1]) {
105203dae382Schristos uint32_t t;
10536659b0b8Smrg t = edges[i];
10546659b0b8Smrg edges[i] = edges[i - 1];
10556659b0b8Smrg edges[i - 1] = t;
10566659b0b8Smrg n++;
10576659b0b8Smrg }
10586659b0b8Smrg }
10596659b0b8Smrg } while (n > 0);
106003dae382Schristos
106103dae382Schristos for (i = 1; i < j; i++) {
106203dae382Schristos if (edges[i] != edges[n]) {
10636659b0b8Smrg n++;
106403dae382Schristos if (n != i)
106503dae382Schristos edges[n] = edges[i];
10666659b0b8Smrg }
106703dae382Schristos }
106803dae382Schristos
106903dae382Schristos n++;
107003dae382Schristos for (i = 0; i < NPART; i++) {
107103dae382Schristos if (p[i].nblk > 0) {
107203dae382Schristos for (j = 0; j < n; j++) {
107303dae382Schristos if ((p[i].startcyl <= edges[j]) &&
107403dae382Schristos (p[i].endcyl > edges[j])) {
107503dae382Schristos table[j][i] = 1;
107603dae382Schristos } else {
107703dae382Schristos table[j][i] = 0;
10786659b0b8Smrg }
10796659b0b8Smrg }
10806659b0b8Smrg }
10816659b0b8Smrg }
108203dae382Schristos
10836659b0b8Smrg ncols = screen_columns() - 2;
108403dae382Schristos for (i = 0; i < n; i++)
108503dae382Schristos ce[i] = (edges[i] * ncols) / (double) edges[n - 1];
108603dae382Schristos
108703dae382Schristos for (i = 1; i < n; i++)
108803dae382Schristos if (ce[i] <= ce[i - 1])
108903dae382Schristos ce[i] = ce[i - 1] + 1;
109003dae382Schristos
10911638ad27Slukem if ((size_t)ce[n - 1] > ncols) {
109203dae382Schristos ce[n - 1] = ncols;
109303dae382Schristos for (i = n - 1; (i > 0) && (ce[i] <= ce[i - 1]); i--)
109403dae382Schristos ce[i - 1] = ce[i] - 1;
109503dae382Schristos if (ce[0] < 0)
109603dae382Schristos for (i = 0; i < n; i++)
109703dae382Schristos ce[i] = i;
10986659b0b8Smrg }
109903dae382Schristos
11006659b0b8Smrg printf("\n");
110103dae382Schristos for (i = 0; i < NPART; i++) {
110203dae382Schristos if (p[i].nblk > 0) {
110303dae382Schristos r = -1;
110403dae382Schristos do {
110503dae382Schristos r++;
110603dae382Schristos for (j = i - 1; j >= 0; j--) {
110703dae382Schristos if (row[j] != r)
110803dae382Schristos continue;
110903dae382Schristos for (k = 0; k < n; k++)
111003dae382Schristos if (table[k][i] && table[k][j])
111103dae382Schristos break;
111203dae382Schristos if (k < n)
111303dae382Schristos break;
11146659b0b8Smrg }
11156659b0b8Smrg } while (j >= 0);
11166659b0b8Smrg row[i] = r;
111703dae382Schristos } else {
111803dae382Schristos row[i] = -1;
11196659b0b8Smrg }
11206659b0b8Smrg }
11216659b0b8Smrg r = row[0];
112203dae382Schristos for (i = 1; i < NPART; i++)
112303dae382Schristos if (row[i] > r)
112403dae382Schristos r = row[i];
112503dae382Schristos
112603dae382Schristos if ((line = malloc(ncols + 1)) == NULL)
112703dae382Schristos err(1, "Can't allocate memory");
112803dae382Schristos
112903dae382Schristos for (i = 0; i <= r; i++) {
11301638ad27Slukem for (j = 0; (size_t)j < ncols; j++)
113103dae382Schristos line[j] = ' ';
113203dae382Schristos for (j = 0; j < NPART; j++) {
113303dae382Schristos if (row[j] != i)
113403dae382Schristos continue;
11356659b0b8Smrg k = 0;
113603dae382Schristos for (k = 0; k < n; k++) {
113703dae382Schristos if (table[k][j]) {
113803dae382Schristos for (c = ce[k]; c < ce[k + 1]; c++)
113903dae382Schristos line[c] = 'a' + j;
11406659b0b8Smrg }
11416659b0b8Smrg }
11426659b0b8Smrg }
11436659b0b8Smrg for (j = ncols - 1; (j >= 0) && (line[j] == ' '); j--);
11446659b0b8Smrg printf("%.*s\n", j + 1, line);
11456659b0b8Smrg }
11466659b0b8Smrg free(line);
11476659b0b8Smrg }
11486659b0b8Smrg
11496659b0b8Smrg #ifdef S_COMMAND
11506659b0b8Smrg /*
11516659b0b8Smrg * This computes an appropriate checksum for an in-core label. It's
11526659b0b8Smrg * not really related to the S command, except that it's needed only
11536659b0b8Smrg * by setlabel(), which is #ifdef S_COMMAND.
11546659b0b8Smrg */
115503dae382Schristos static unsigned short int
dkcksum(const struct disklabel * lp)115603dae382Schristos dkcksum(const struct disklabel *lp)
11576659b0b8Smrg {
11586659b0b8Smrg const unsigned short int *start;
11596659b0b8Smrg const unsigned short int *end;
11606659b0b8Smrg unsigned short int sum;
11616659b0b8Smrg const unsigned short int *p;
11626659b0b8Smrg
11636659b0b8Smrg start = (const void *)lp;
11646659b0b8Smrg end = (const void *)&lp->d_partitions[lp->d_npartitions];
11656659b0b8Smrg sum = 0;
116603dae382Schristos for (p = start; p < end; p++)
116703dae382Schristos sum ^= *p;
11686659b0b8Smrg return (sum);
11696659b0b8Smrg }
11706659b0b8Smrg
11716659b0b8Smrg /*
11726659b0b8Smrg * Set the in-core label. This is basically putlabel, except it builds
11736659b0b8Smrg * a struct disklabel instead of a Sun label buffer, and uses
11746659b0b8Smrg * DIOCSDINFO instead of lseek-and-write.
11756659b0b8Smrg */
117603dae382Schristos static void
setlabel(void)117703dae382Schristos setlabel(void)
11786659b0b8Smrg {
11796659b0b8Smrg union {
11806659b0b8Smrg struct disklabel l;
11816659b0b8Smrg char pad[sizeof(struct disklabel) -
11826659b0b8Smrg (MAXPARTITIONS * sizeof(struct partition)) +
11836659b0b8Smrg (16 * sizeof(struct partition))];
11846659b0b8Smrg } u;
11856659b0b8Smrg int i;
118603dae382Schristos struct part *p = label.partitions;
11876659b0b8Smrg
118803dae382Schristos if (ioctl(diskfd, DIOCGDINFO, &u.l) == -1) {
118903dae382Schristos warn("ioctl DIOCGDINFO failed");
11906659b0b8Smrg return;
11916659b0b8Smrg }
119203dae382Schristos if (u.l.d_secsize != 512) {
1193eda9e509Sgrant warnx("Disk claims %d-byte sectors", (int)u.l.d_secsize);
11946659b0b8Smrg }
11956659b0b8Smrg u.l.d_nsectors = label.nsect;
11966659b0b8Smrg u.l.d_ntracks = label.nhead;
11976659b0b8Smrg u.l.d_ncylinders = label.ncyl;
11986659b0b8Smrg u.l.d_secpercyl = label.nsect * label.nhead;
11996659b0b8Smrg u.l.d_rpm = label.rpm;
12006659b0b8Smrg u.l.d_interleave = label.intrlv;
12014ef0e6e1Smrg u.l.d_npartitions = getmaxpartitions();
120203dae382Schristos memset(&u.l.d_partitions[0], 0,
120303dae382Schristos u.l.d_npartitions * sizeof(struct partition));
120403dae382Schristos for (i = 0; i < u.l.d_npartitions; i++) {
120503dae382Schristos u.l.d_partitions[i].p_size = p[i].nblk;
120603dae382Schristos u.l.d_partitions[i].p_offset = p[i].startcyl
120703dae382Schristos * label.nsect * label.nhead;
12086659b0b8Smrg u.l.d_partitions[i].p_fsize = 0;
12096659b0b8Smrg u.l.d_partitions[i].p_fstype = (i == 1) ? FS_SWAP :
121003dae382Schristos (i == 2) ? FS_UNUSED : FS_BSDFFS;
12116659b0b8Smrg u.l.d_partitions[i].p_frag = 0;
12126659b0b8Smrg u.l.d_partitions[i].p_cpg = 0;
12136659b0b8Smrg }
12146659b0b8Smrg u.l.d_checksum = 0;
12156659b0b8Smrg u.l.d_checksum = dkcksum(&u.l);
121603dae382Schristos if (ioctl(diskfd, DIOCSDINFO, &u.l) == -1) {
121703dae382Schristos warn("ioctl DIOCSDINFO failed");
12186659b0b8Smrg return;
12196659b0b8Smrg }
12206659b0b8Smrg }
12216659b0b8Smrg #endif
12226659b0b8Smrg
122303dae382Schristos static const char *help[] = {
1224133ea6e5Slukem "?\t- print this help",
1225133ea6e5Slukem "L\t- print label, except for partition table",
1226133ea6e5Slukem "P\t- print partition table",
1227133ea6e5Slukem "PP\t- print partition table including size=0 offset=0 entries",
122803dae382Schristos "[abcdefghijklmnop] <cylno> <size> - change partition",
122903dae382Schristos "V <name> <value> - change a non-partition label value",
1230133ea6e5Slukem "W\t- write (possibly modified) label out",
123103dae382Schristos #ifdef S_COMMAND
1232133ea6e5Slukem "S\t- set label in the kernel (orthogonal to W)",
123303dae382Schristos #endif
1234133ea6e5Slukem "Q\t- quit program (error if no write since last change)",
1235133ea6e5Slukem "Q!\t- quit program (unconditionally) [EOF also quits]",
123603dae382Schristos NULL
123703dae382Schristos };
123803dae382Schristos
12396659b0b8Smrg /*
12406659b0b8Smrg * Read and execute one command line from the user.
12416659b0b8Smrg */
124203dae382Schristos static void
docmd(void)124303dae382Schristos docmd(void)
12446659b0b8Smrg {
12456659b0b8Smrg char cmdline[512];
124603dae382Schristos int i;
12476659b0b8Smrg
124803dae382Schristos if (!quiet)
124903dae382Schristos printf("sunlabel> ");
125003dae382Schristos if (fgets(&cmdline[0], sizeof(cmdline), stdin) != &cmdline[0])
125103dae382Schristos exit(0);
125203dae382Schristos switch (cmdline[0]) {
125303dae382Schristos case '?':
125403dae382Schristos for (i = 0; help[i]; i++)
125503dae382Schristos printf("%s\n", help[i]);
12566659b0b8Smrg break;
12576659b0b8Smrg case 'L':
12586659b0b8Smrg print_label();
12596659b0b8Smrg break;
12606659b0b8Smrg case 'P':
12616659b0b8Smrg print_part(cmdline[1] == 'P');
12626659b0b8Smrg break;
12636659b0b8Smrg case 'W':
12646659b0b8Smrg putlabel();
12656659b0b8Smrg break;
12666659b0b8Smrg case 'S':
12676659b0b8Smrg #ifdef S_COMMAND
12686659b0b8Smrg setlabel();
12696659b0b8Smrg #else
12706659b0b8Smrg printf("This compilation doesn't support S.\n");
12716659b0b8Smrg #endif
12726659b0b8Smrg break;
12736659b0b8Smrg case 'Q':
127403dae382Schristos if ((cmdline[1] == '!') || !label.dirty)
127503dae382Schristos exit(0);
127603dae382Schristos printf("Label is dirty - use w to write it\n");
127703dae382Schristos printf("Use Q! to quit anyway.\n");
12786659b0b8Smrg break;
127903dae382Schristos case 'a':
128003dae382Schristos case 'b':
128103dae382Schristos case 'c':
128203dae382Schristos case 'd':
128303dae382Schristos case 'e':
128403dae382Schristos case 'f':
128503dae382Schristos case 'g':
128603dae382Schristos case 'h':
128703dae382Schristos case 'i':
128803dae382Schristos case 'j':
128903dae382Schristos case 'k':
129003dae382Schristos case 'l':
129103dae382Schristos case 'm':
129203dae382Schristos case 'n':
129303dae382Schristos case 'o':
129403dae382Schristos case 'p':
12956659b0b8Smrg chpart(LETTERPART(cmdline[0]), &cmdline[1]);
12966659b0b8Smrg break;
12976659b0b8Smrg case 'V':
12986659b0b8Smrg chvalue(&cmdline[1]);
12996659b0b8Smrg break;
13006659b0b8Smrg case '\n':
13016659b0b8Smrg break;
13026659b0b8Smrg default:
130303dae382Schristos printf("(Unrecognized command character %c ignored.)\n",
130403dae382Schristos cmdline[0]);
13056659b0b8Smrg break;
13066659b0b8Smrg }
13076659b0b8Smrg }
1308133ea6e5Slukem
13096659b0b8Smrg /*
13106659b0b8Smrg * main() (duh!). Pretty boring.
13116659b0b8Smrg */
131203dae382Schristos int
main(int ac,char ** av)131303dae382Schristos main(int ac, char **av)
13146659b0b8Smrg {
13156659b0b8Smrg handleargs(ac, av);
13166659b0b8Smrg getlabel();
131703dae382Schristos for (;;)
131803dae382Schristos docmd();
13196659b0b8Smrg }
1320