]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/fdisk/fdisk.c
This commit was generated by cvs2svn to compensate for changes in r170331,
[FreeBSD/FreeBSD.git] / sbin / fdisk / fdisk.c
1 /*
2  * Mach Operating System
3  * Copyright (c) 1992 Carnegie Mellon University
4  * All Rights Reserved.
5  *
6  * Permission to use, copy, modify and distribute this software and its
7  * documentation is hereby granted, provided that both the copyright
8  * notice and this permission notice appear in all copies of the
9  * software, derivative works or modified versions, and any portions
10  * thereof, and that both notices appear in supporting documentation.
11  *
12  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
13  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
15  *
16  * Carnegie Mellon requests users of this software to return to
17  *
18  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
19  *  School of Computer Science
20  *  Carnegie Mellon University
21  *  Pittsburgh PA 15213-3890
22  *
23  * any improvements or extensions that they make and grant Carnegie Mellon
24  * the rights to redistribute these changes.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/disk.h>
31 #include <sys/disklabel.h>
32 #include <sys/diskmbr.h>
33 #include <sys/endian.h>
34 #include <sys/param.h>
35 #include <sys/stat.h>
36 #include <sys/mount.h>
37 #include <ctype.h>
38 #include <fcntl.h>
39 #include <err.h>
40 #include <errno.h>
41 #include <libgeom.h>
42 #include <paths.h>
43 #include <regex.h>
44 #include <stdint.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49
50 int iotest;
51
52 #define LBUF 100
53 static char lbuf[LBUF];
54
55 /*
56  *
57  * Ported to 386bsd by Julian Elischer  Thu Oct 15 20:26:46 PDT 1992
58  *
59  * 14-Dec-89  Robert Baron (rvb) at Carnegie-Mellon University
60  *      Copyright (c) 1989      Robert. V. Baron
61  *      Created.
62  */
63
64 #define Decimal(str, ans, tmp) if (decimal(str, &tmp, ans)) ans = tmp
65
66 #define RoundCyl(x) ((((x) + cylsecs - 1) / cylsecs) * cylsecs)
67
68 #define MAX_SEC_SIZE 2048       /* maximum section size that is supported */
69 #define MIN_SEC_SIZE 512        /* the sector size to start sensing at */
70 static int secsize = 0;         /* the sensed sector size */
71
72 static char *disk;
73
74 static int cyls, sectors, heads, cylsecs, disksecs;
75
76 struct mboot {
77         unsigned char padding[2]; /* force the longs to be long aligned */
78         unsigned char *bootinst;  /* boot code */
79         off_t bootinst_size;
80         struct  dos_partition parts[NDOSPART];
81 };
82
83 static struct mboot mboot;
84 static int fd;
85
86 #define ACTIVE 0x80
87
88 static uint dos_cyls;
89 static uint dos_heads;
90 static uint dos_sectors;
91 static uint dos_cylsecs;
92
93 #define DOSSECT(s,c) ((s & 0x3f) | ((c >> 2) & 0xc0))
94 #define DOSCYL(c)       (c & 0xff)
95
96 #define MAX_ARGS        10
97
98 static int      current_line_number;
99
100 static int      geom_processed = 0;
101 static int      part_processed = 0;
102 static int      active_processed = 0;
103
104 typedef struct cmd {
105     char                cmd;
106     int                 n_args;
107     struct arg {
108         char    argtype;
109         int     arg_val;
110     }                   args[MAX_ARGS];
111 } CMD;
112
113 static int B_flag  = 0;         /* replace boot code */
114 static int I_flag  = 0;         /* use entire disk for FreeBSD */
115 static int a_flag  = 0;         /* set active partition */
116 static char *b_flag = NULL;     /* path to boot code */
117 static int i_flag  = 0;         /* replace partition data */
118 static int u_flag  = 0;         /* update partition data */
119 static int s_flag  = 0;         /* Print a summary and exit */
120 static int t_flag  = 0;         /* test only */
121 static char *f_flag = NULL;     /* Read config info from file */
122 static int v_flag  = 0;         /* Be verbose */
123 static int print_config_flag = 0;
124
125 static struct part_type
126 {
127         unsigned char type;
128         const char *name;
129 } part_types[] = {
130          {0x00, "unused"}
131         ,{0x01, "Primary DOS with 12 bit FAT"}
132         ,{0x02, "XENIX / file system"}
133         ,{0x03, "XENIX /usr file system"}
134         ,{0x04, "Primary DOS with 16 bit FAT (< 32MB)"}
135         ,{0x05, "Extended DOS"}
136         ,{0x06, "Primary 'big' DOS (>= 32MB)"}
137         ,{0x07, "OS/2 HPFS, NTFS, QNX-2 (16 bit) or Advanced UNIX"}
138         ,{0x08, "AIX file system or SplitDrive"}
139         ,{0x09, "AIX boot partition or Coherent"}
140         ,{0x0A, "OS/2 Boot Manager, OPUS or Coherent swap"}
141         ,{0x0B, "DOS or Windows 95 with 32 bit FAT"}
142         ,{0x0C, "DOS or Windows 95 with 32 bit FAT (LBA)"}
143         ,{0x0E, "Primary 'big' DOS (>= 32MB, LBA)"}
144         ,{0x0F, "Extended DOS (LBA)"}
145         ,{0x10, "OPUS"}
146         ,{0x11, "OS/2 BM: hidden DOS with 12-bit FAT"}
147         ,{0x12, "Compaq diagnostics"}
148         ,{0x14, "OS/2 BM: hidden DOS with 16-bit FAT (< 32MB)"}
149         ,{0x16, "OS/2 BM: hidden DOS with 16-bit FAT (>= 32MB)"}
150         ,{0x17, "OS/2 BM: hidden IFS (e.g. HPFS)"}
151         ,{0x18, "AST Windows swapfile"}
152         ,{0x24, "NEC DOS"}
153         ,{0x3C, "PartitionMagic recovery"}
154         ,{0x39, "plan9"}
155         ,{0x40, "VENIX 286"}
156         ,{0x41, "Linux/MINIX (sharing disk with DRDOS)"}
157         ,{0x42, "SFS or Linux swap (sharing disk with DRDOS)"}
158         ,{0x43, "Linux native (sharing disk with DRDOS)"}
159         ,{0x4D, "QNX 4.2 Primary"}
160         ,{0x4E, "QNX 4.2 Secondary"}
161         ,{0x4F, "QNX 4.2 Tertiary"}
162         ,{0x50, "DM (disk manager)"}
163         ,{0x51, "DM6 Aux1 (or Novell)"}
164         ,{0x52, "CP/M or Microport SysV/AT"}
165         ,{0x53, "DM6 Aux3"}
166         ,{0x54, "DM6"}
167         ,{0x55, "EZ-Drive (disk manager)"}
168         ,{0x56, "Golden Bow (disk manager)"}
169         ,{0x5c, "Priam Edisk (disk manager)"} /* according to S. Widlake */
170         ,{0x61, "SpeedStor"}
171         ,{0x63, "System V/386 (such as ISC UNIX), GNU HURD or Mach"}
172         ,{0x64, "Novell Netware/286 2.xx"}
173         ,{0x65, "Novell Netware/386 3.xx"}
174         ,{0x70, "DiskSecure Multi-Boot"}
175         ,{0x75, "PCIX"}
176         ,{0x77, "QNX4.x"}
177         ,{0x78, "QNX4.x 2nd part"}
178         ,{0x79, "QNX4.x 3rd part"}
179         ,{0x80, "Minix until 1.4a"}
180         ,{0x81, "Minix since 1.4b, early Linux partition or Mitac disk manager"}
181         ,{0x82, "Linux swap or Solaris x86"}
182         ,{0x83, "Linux native"}
183         ,{0x84, "OS/2 hidden C: drive"}
184         ,{0x85, "Linux extended"}
185         ,{0x86, "NTFS volume set??"}
186         ,{0x87, "NTFS volume set??"}
187         ,{0x93, "Amoeba file system"}
188         ,{0x94, "Amoeba bad block table"}
189         ,{0x9F, "BSD/OS"}
190         ,{0xA0, "Suspend to Disk"}
191         ,{0xA5, "FreeBSD/NetBSD/386BSD"}
192         ,{0xA6, "OpenBSD"}
193         ,{0xA7, "NeXTSTEP"}
194         ,{0xA9, "NetBSD"}
195         ,{0xAC, "IBM JFS"}
196         ,{0xAF, "HFS+"}
197         ,{0xB7, "BSDI BSD/386 file system"}
198         ,{0xB8, "BSDI BSD/386 swap"}
199         ,{0xBE, "Solaris x86 boot"}
200         ,{0xBF, "Solaris x86 (new)"}
201         ,{0xC1, "DRDOS/sec with 12-bit FAT"}
202         ,{0xC4, "DRDOS/sec with 16-bit FAT (< 32MB)"}
203         ,{0xC6, "DRDOS/sec with 16-bit FAT (>= 32MB)"}
204         ,{0xC7, "Syrinx"}
205         ,{0xDB, "CP/M, Concurrent CP/M, Concurrent DOS or CTOS"}
206         ,{0xE1, "DOS access or SpeedStor with 12-bit FAT extended partition"}
207         ,{0xE3, "DOS R/O or SpeedStor"}
208         ,{0xE4, "SpeedStor with 16-bit FAT extended partition < 1024 cyl."}
209         ,{0xEB, "BeOS file system"}
210         ,{0xEE, "EFI GPT"}
211         ,{0xEF, "EFI System Partition"}
212         ,{0xF1, "SpeedStor"}
213         ,{0xF2, "DOS 3.3+ Secondary"}
214         ,{0xF4, "SpeedStor large partition"}
215         ,{0xFE, "SpeedStor >1024 cyl. or LANstep"}
216         ,{0xFF, "Xenix bad blocks table"}
217 };
218
219 static void print_s0(int which);
220 static void print_part(int i);
221 static void init_sector0(unsigned long start);
222 static void init_boot(void);
223 static void change_part(int i);
224 static void print_params(void);
225 static void change_active(int which);
226 static void change_code(void);
227 static void get_params_to_use(void);
228 static char *get_rootdisk(void);
229 static void dos(struct dos_partition *partp);
230 static int open_disk(int flag);
231 static ssize_t read_disk(off_t sector, void *buf);
232 static int write_disk(off_t sector, void *buf);
233 static int get_params(void);
234 static int read_s0(void);
235 static int write_s0(void);
236 static int ok(const char *str);
237 static int decimal(const char *str, int *num, int deflt);
238 static const char *get_type(int type);
239 static int read_config(char *config_file);
240 static void reset_boot(void);
241 static int sanitize_partition(struct dos_partition *);
242 static void usage(void);
243
244 int
245 main(int argc, char *argv[])
246 {
247         struct  stat sb;
248         int     c, i;
249         int     partition = -1;
250         struct  dos_partition *partp;
251
252         while ((c = getopt(argc, argv, "BIab:f:ipstuv1234")) != -1)
253                 switch (c) {
254                 case 'B':
255                         B_flag = 1;
256                         break;
257                 case 'I':
258                         I_flag = 1;
259                         break;
260                 case 'a':
261                         a_flag = 1;
262                         break;
263                 case 'b':
264                         b_flag = optarg;
265                         break;
266                 case 'f':
267                         f_flag = optarg;
268                         break;
269                 case 'i':
270                         i_flag = 1;
271                         break;
272                 case 'p':
273                         print_config_flag = 1;
274                         break;
275                 case 's':
276                         s_flag = 1;
277                         break;
278                 case 't':
279                         t_flag = 1;
280                         break;
281                 case 'u':
282                         u_flag = 1;
283                         break;
284                 case 'v':
285                         v_flag = 1;
286                         break;
287                 case '1':
288                 case '2':
289                 case '3':
290                 case '4':
291                         partition = c - '0';
292                         break;
293                 default:
294                         usage();
295                 }
296         if (f_flag || i_flag)
297                 u_flag = 1;
298         if (t_flag)
299                 v_flag = 1;
300         argc -= optind;
301         argv += optind;
302
303         if (argc == 0) {
304                 disk = get_rootdisk();
305         } else {
306                 if (stat(argv[0], &sb) == 0) {
307                         /* OK, full pathname given */
308                         disk = argv[0];
309                 } else if (errno == ENOENT && argv[0][0] != '/') {
310                         /* Try prepending "/dev" */
311                         asprintf(&disk, "%s%s", _PATH_DEV, argv[0]);
312                         if (disk == NULL)
313                                 errx(1, "out of memory");
314                 } else {
315                         /* other stat error, let it fail below */
316                         disk = argv[0];
317                 }
318         }
319         if (open_disk(u_flag) < 0)
320                 err(1, "cannot open disk %s", disk);
321
322         /* (abu)use mboot.bootinst to probe for the sector size */
323         if ((mboot.bootinst = malloc(MAX_SEC_SIZE)) == NULL)
324                 err(1, "cannot allocate buffer to determine disk sector size");
325         if (read_disk(0, mboot.bootinst) == -1)
326                 errx(1, "could not detect sector size");
327         free(mboot.bootinst);
328         mboot.bootinst = NULL;
329
330         if (print_config_flag) {
331                 if (read_s0())
332                         err(1, "read_s0");
333
334                 printf("# %s\n", disk);
335                 printf("g c%d h%d s%d\n", dos_cyls, dos_heads, dos_sectors);
336
337                 for (i = 0; i < NDOSPART; i++) {
338                         partp = ((struct dos_partition *)&mboot.parts) + i;
339
340                         if (partp->dp_start == 0 && partp->dp_size == 0)
341                                 continue;
342
343                         printf("p %d 0x%02x %lu %lu\n", i + 1, partp->dp_typ,
344                             (u_long)partp->dp_start, (u_long)partp->dp_size);
345
346                         /* Fill flags for the partition. */
347                         if (partp->dp_flag & 0x80)
348                                 printf("a %d\n", i + 1);
349                 }
350                 exit(0);
351         }
352         if (s_flag) {
353                 if (read_s0())
354                         err(1, "read_s0");
355                 printf("%s: %d cyl %d hd %d sec\n", disk, dos_cyls, dos_heads,
356                     dos_sectors);
357                 printf("Part  %11s %11s Type Flags\n", "Start", "Size");
358                 for (i = 0; i < NDOSPART; i++) {
359                         partp = ((struct dos_partition *) &mboot.parts) + i;
360                         if (partp->dp_start == 0 && partp->dp_size == 0)
361                                 continue;
362                         printf("%4d: %11lu %11lu 0x%02x 0x%02x\n", i + 1,
363                             (u_long) partp->dp_start,
364                             (u_long) partp->dp_size, partp->dp_typ,
365                             partp->dp_flag);
366                 }
367                 exit(0);
368         }
369
370         printf("******* Working on device %s *******\n",disk);
371
372         if (I_flag) {
373                 read_s0();
374                 reset_boot();
375                 partp = (struct dos_partition *) (&mboot.parts[0]);
376                 partp->dp_typ = DOSPTYP_386BSD;
377                 partp->dp_flag = ACTIVE;
378                 partp->dp_start = dos_sectors;
379                 partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs -
380                     dos_sectors;
381                 dos(partp);
382                 if (v_flag)
383                         print_s0(-1);
384                 if (!t_flag)
385                         write_s0();
386                 exit(0);
387         }
388         if (f_flag) {
389             if (read_s0() || i_flag)
390                 reset_boot();
391             if (!read_config(f_flag))
392                 exit(1);
393             if (v_flag)
394                 print_s0(-1);
395             if (!t_flag)
396                 write_s0();
397         } else {
398             if(u_flag)
399                 get_params_to_use();
400             else
401                 print_params();
402
403             if (read_s0())
404                 init_sector0(dos_sectors);
405
406             printf("Media sector size is %d\n", secsize);
407             printf("Warning: BIOS sector numbering starts with sector 1\n");
408             printf("Information from DOS bootblock is:\n");
409             if (partition == -1)
410                 for (i = 1; i <= NDOSPART; i++)
411                     change_part(i);
412             else
413                 change_part(partition);
414
415             if (u_flag || a_flag)
416                 change_active(partition);
417
418             if (B_flag)
419                 change_code();
420
421             if (u_flag || a_flag || B_flag) {
422                 if (!t_flag) {
423                     printf("\nWe haven't changed the partition table yet.  ");
424                     printf("This is your last chance.\n");
425                 }
426                 print_s0(-1);
427                 if (!t_flag) {
428                     if (ok("Should we write new partition table?"))
429                         write_s0();
430                 } else {
431                     printf("\n-t flag specified -- partition table not written.\n");
432                 }
433             }
434         }
435
436         exit(0);
437 }
438
439 static void
440 usage()
441 {
442         fprintf(stderr, "%s%s",
443                 "usage: fdisk [-BIaipstu] [-b bootcode] [-1234] [disk]\n",
444                 "       fdisk -f configfile [-itv] [disk]\n");
445         exit(1);
446 }
447
448 static void
449 print_s0(int which)
450 {
451         int     i;
452
453         print_params();
454         printf("Information from DOS bootblock is:\n");
455         if (which == -1)
456                 for (i = 1; i <= NDOSPART; i++)
457                         printf("%d: ", i), print_part(i);
458         else
459                 print_part(which);
460 }
461
462 static struct dos_partition mtpart;
463
464 static void
465 print_part(int i)
466 {
467         struct    dos_partition *partp;
468         u_int64_t part_mb;
469
470         partp = ((struct dos_partition *) &mboot.parts) + i - 1;
471
472         if (!bcmp(partp, &mtpart, sizeof (struct dos_partition))) {
473                 printf("<UNUSED>\n");
474                 return;
475         }
476         /*
477          * Be careful not to overflow.
478          */
479         part_mb = partp->dp_size;
480         part_mb *= secsize;
481         part_mb /= (1024 * 1024);
482         printf("sysid %d (%#04x),(%s)\n", partp->dp_typ, partp->dp_typ,
483             get_type(partp->dp_typ));
484         printf("    start %lu, size %lu (%ju Meg), flag %x%s\n",
485                 (u_long)partp->dp_start,
486                 (u_long)partp->dp_size, 
487                 (uintmax_t)part_mb,
488                 partp->dp_flag,
489                 partp->dp_flag == ACTIVE ? " (active)" : "");
490         printf("\tbeg: cyl %d/ head %d/ sector %d;\n\tend: cyl %d/ head %d/ sector %d\n"
491                 ,DPCYL(partp->dp_scyl, partp->dp_ssect)
492                 ,partp->dp_shd
493                 ,DPSECT(partp->dp_ssect)
494                 ,DPCYL(partp->dp_ecyl, partp->dp_esect)
495                 ,partp->dp_ehd
496                 ,DPSECT(partp->dp_esect));
497 }
498
499
500 static void
501 init_boot(void)
502 {
503 #ifndef __ia64__
504         const char *fname;
505         int fdesc, n;
506         struct stat sb;
507
508         fname = b_flag ? b_flag : "/boot/mbr";
509         if ((fdesc = open(fname, O_RDONLY)) == -1 ||
510             fstat(fdesc, &sb) == -1)
511                 err(1, "%s", fname);
512         if ((mboot.bootinst_size = sb.st_size) % secsize != 0)
513                 errx(1, "%s: length must be a multiple of sector size", fname);
514         if (mboot.bootinst != NULL)
515                 free(mboot.bootinst);
516         if ((mboot.bootinst = malloc(mboot.bootinst_size = sb.st_size)) == NULL)
517                 errx(1, "%s: unable to allocate read buffer", fname);
518         if ((n = read(fdesc, mboot.bootinst, mboot.bootinst_size)) == -1 ||
519             close(fdesc))
520                 err(1, "%s", fname);
521         if (n != mboot.bootinst_size)
522                 errx(1, "%s: short read", fname);
523 #else
524         if (mboot.bootinst != NULL)
525                 free(mboot.bootinst);
526         mboot.bootinst_size = secsize;
527         if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL)
528                 errx(1, "unable to allocate boot block buffer");
529         memset(mboot.bootinst, 0, mboot.bootinst_size);
530         le16enc(&mboot.bootinst[DOSMAGICOFFSET], DOSMAGIC);
531 #endif
532 }
533
534
535 static void
536 init_sector0(unsigned long start)
537 {
538         struct dos_partition *partp = (struct dos_partition *) (&mboot.parts[0]);
539
540         init_boot();
541
542         partp->dp_typ = DOSPTYP_386BSD;
543         partp->dp_flag = ACTIVE;
544         start = ((start + dos_sectors - 1) / dos_sectors) * dos_sectors;
545         if(start == 0)
546                 start = dos_sectors;
547         partp->dp_start = start;
548         partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs - start;
549
550         dos(partp);
551 }
552
553 static void
554 change_part(int i)
555 {
556         struct dos_partition *partp = ((struct dos_partition *) &mboot.parts) + i - 1;
557
558     printf("The data for partition %d is:\n", i);
559     print_part(i);
560
561     if (u_flag && ok("Do you want to change it?")) {
562         int tmp;
563
564         if (i_flag) {
565                 bzero((char *)partp, sizeof (struct dos_partition));
566                 if (i == 1) {
567                         init_sector0(1);
568                         printf("\nThe static data for the slice 1 has been reinitialized to:\n");
569                         print_part(i);
570                 }
571         }
572
573         do {
574                 Decimal("sysid (165=FreeBSD)", partp->dp_typ, tmp);
575                 Decimal("start", partp->dp_start, tmp);
576                 Decimal("size", partp->dp_size, tmp);
577                 if (!sanitize_partition(partp)) {
578                         warnx("ERROR: failed to adjust; setting sysid to 0");
579                         partp->dp_typ = 0;
580                 }
581
582                 if (ok("Explicitly specify beg/end address ?"))
583                 {
584                         int     tsec,tcyl,thd;
585                         tcyl = DPCYL(partp->dp_scyl,partp->dp_ssect);
586                         thd = partp->dp_shd;
587                         tsec = DPSECT(partp->dp_ssect);
588                         Decimal("beginning cylinder", tcyl, tmp);
589                         Decimal("beginning head", thd, tmp);
590                         Decimal("beginning sector", tsec, tmp);
591                         partp->dp_scyl = DOSCYL(tcyl);
592                         partp->dp_ssect = DOSSECT(tsec,tcyl);
593                         partp->dp_shd = thd;
594
595                         tcyl = DPCYL(partp->dp_ecyl,partp->dp_esect);
596                         thd = partp->dp_ehd;
597                         tsec = DPSECT(partp->dp_esect);
598                         Decimal("ending cylinder", tcyl, tmp);
599                         Decimal("ending head", thd, tmp);
600                         Decimal("ending sector", tsec, tmp);
601                         partp->dp_ecyl = DOSCYL(tcyl);
602                         partp->dp_esect = DOSSECT(tsec,tcyl);
603                         partp->dp_ehd = thd;
604                 } else
605                         dos(partp);
606
607                 print_part(i);
608         } while (!ok("Are we happy with this entry?"));
609     }
610 }
611
612 static void
613 print_params()
614 {
615         printf("parameters extracted from in-core disklabel are:\n");
616         printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
617                         ,cyls,heads,sectors,cylsecs);
618         if (dos_cyls > 1023 || dos_heads > 255 || dos_sectors > 63)
619                 printf("Figures below won't work with BIOS for partitions not in cyl 1\n");
620         printf("parameters to be used for BIOS calculations are:\n");
621         printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
622                 ,dos_cyls,dos_heads,dos_sectors,dos_cylsecs);
623 }
624
625 static void
626 change_active(int which)
627 {
628         struct dos_partition *partp = &mboot.parts[0];
629         int active, i, new, tmp;
630
631         active = -1;
632         for (i = 0; i < NDOSPART; i++) {
633                 if ((partp[i].dp_flag & ACTIVE) == 0)
634                         continue;
635                 printf("Partition %d is marked active\n", i + 1);
636                 if (active == -1)
637                         active = i + 1;
638         }
639         if (a_flag && which != -1)
640                 active = which;
641         else if (active == -1)
642                 active = 1;
643
644         if (!ok("Do you want to change the active partition?"))
645                 return;
646 setactive:
647         do {
648                 new = active;
649                 Decimal("active partition", new, tmp);
650                 if (new < 1 || new > 4) {
651                         printf("Active partition number must be in range 1-4."
652                                         "  Try again.\n");
653                         goto setactive;
654                 }
655                 active = new;
656         } while (!ok("Are you happy with this choice"));
657         for (i = 0; i < NDOSPART; i++)
658                 partp[i].dp_flag = 0;
659         if (active > 0 && active <= NDOSPART)
660                 partp[active-1].dp_flag = ACTIVE;
661 }
662
663 static void
664 change_code()
665 {
666         if (ok("Do you want to change the boot code?"))
667                 init_boot();
668 }
669
670 void
671 get_params_to_use()
672 {
673         int     tmp;
674         print_params();
675         if (ok("Do you want to change our idea of what BIOS thinks ?"))
676         {
677                 do
678                 {
679                         Decimal("BIOS's idea of #cylinders", dos_cyls, tmp);
680                         Decimal("BIOS's idea of #heads", dos_heads, tmp);
681                         Decimal("BIOS's idea of #sectors", dos_sectors, tmp);
682                         dos_cylsecs = dos_heads * dos_sectors;
683                         print_params();
684                 }
685                 while(!ok("Are you happy with this choice"));
686         }
687 }
688
689
690 /***********************************************\
691 * Change real numbers into strange dos numbers  *
692 \***********************************************/
693 static void
694 dos(struct dos_partition *partp)
695 {
696         int cy, sec;
697         u_int32_t end;
698
699         if (partp->dp_typ == 0 && partp->dp_start == 0 && partp->dp_size == 0) {
700                 memcpy(partp, &mtpart, sizeof(*partp));
701                 return;
702         }
703
704         /* Start c/h/s. */
705         partp->dp_shd = partp->dp_start % dos_cylsecs / dos_sectors;
706         cy = partp->dp_start / dos_cylsecs;
707         sec = partp->dp_start % dos_sectors + 1;
708         partp->dp_scyl = DOSCYL(cy);
709         partp->dp_ssect = DOSSECT(sec, cy);
710
711         /* End c/h/s. */
712         end = partp->dp_start + partp->dp_size - 1;
713         partp->dp_ehd = end % dos_cylsecs / dos_sectors;
714         cy = end / dos_cylsecs;
715         sec = end % dos_sectors + 1;
716         partp->dp_ecyl = DOSCYL(cy);
717         partp->dp_esect = DOSSECT(sec, cy);
718 }
719
720 static int
721 open_disk(int flag)
722 {
723         struct stat     st;
724         int rwmode;
725
726         if (stat(disk, &st) == -1) {
727                 if (errno == ENOENT)
728                         return -2;
729                 warnx("can't get file status of %s", disk);
730                 return -1;
731         }
732         if ( !(st.st_mode & S_IFCHR) )
733                 warnx("device %s is not character special", disk);
734         rwmode = a_flag || I_flag || B_flag || flag ? O_RDWR : O_RDONLY;
735         fd = open(disk, rwmode);
736         if (fd == -1 && errno == EPERM && rwmode == O_RDWR)
737                 fd = open(disk, O_RDONLY);
738         if (fd == -1 && errno == ENXIO)
739                 return -2;
740         if (fd == -1) {
741                 warnx("can't open device %s", disk);
742                 return -1;
743         }
744         if (get_params() == -1) {
745                 warnx("can't get disk parameters on %s", disk);
746                 return -1;
747         }
748         return fd;
749 }
750
751 static ssize_t
752 read_disk(off_t sector, void *buf)
753 {
754
755         lseek(fd, (sector * 512), 0);
756         if (secsize == 0)
757                 for (secsize = MIN_SEC_SIZE; secsize <= MAX_SEC_SIZE;
758                      secsize *= 2) {
759                         /* try the read */
760                         int size = read(fd, buf, secsize);
761                         if (size == secsize)
762                                 /* it worked so return */
763                                 return secsize;
764                 }
765         else
766                 return read(fd, buf, secsize);
767
768         /* we failed to read at any of the sizes */
769         return -1;
770 }
771
772 static int
773 write_disk(off_t sector, void *buf)
774 {
775         int error;
776         struct gctl_req *grq;
777         const char *q;
778         char fbuf[BUFSIZ];
779         int i, fdw;
780
781         grq = gctl_get_handle();
782         gctl_ro_param(grq, "verb", -1, "write MBR");
783         gctl_ro_param(grq, "class", -1, "MBR");
784         q = strrchr(disk, '/');
785         if (q == NULL)
786                 q = disk;
787         else
788                 q++;
789         gctl_ro_param(grq, "geom", -1, q);
790         gctl_ro_param(grq, "data", secsize, buf);
791         q = gctl_issue(grq);
792         if (q == NULL) {
793                 gctl_free(grq);
794                 return(0);
795         }
796         warnx("%s", q);
797         gctl_free(grq);
798         
799         error = pwrite(fd, buf, secsize, (sector * 512));
800         if (error == secsize)
801                 return (0);
802
803         for (i = 1; i < 5; i++) {
804                 sprintf(fbuf, "%ss%d", disk, i);
805                 fdw = open(fbuf, O_RDWR, 0);
806                 if (fdw < 0)
807                         continue;
808                 error = ioctl(fdw, DIOCSMBR, buf);
809                 close(fdw);
810                 if (error == 0)
811                         return (0);
812         }
813         warnx("Failed to write sector zero");
814         return(EINVAL);
815 }
816
817 static int
818 get_params()
819 {
820         int error;
821         u_int u;
822         off_t o;
823
824         error = ioctl(fd, DIOCGFWSECTORS, &u);
825         if (error == 0)
826                 sectors = dos_sectors = u;
827         else
828                 sectors = dos_sectors = 63;
829
830         error = ioctl(fd, DIOCGFWHEADS, &u);
831         if (error == 0)
832                 heads = dos_heads = u;
833         else
834                 heads = dos_heads = 255;
835
836         dos_cylsecs = cylsecs = heads * sectors;
837         disksecs = cyls * heads * sectors;
838
839         error = ioctl(fd, DIOCGSECTORSIZE, &u);
840         if (error != 0 || u == 0)
841                 u = 512;
842         else
843                 secsize = u;
844
845         error = ioctl(fd, DIOCGMEDIASIZE, &o);
846         if (error == 0) {
847                 disksecs = o / u;
848                 cyls = dos_cyls = o / (u * dos_heads * dos_sectors);
849         }
850
851         return (disksecs);
852 }
853 \f
854
855 static int
856 read_s0()
857 {
858         int i;
859
860         mboot.bootinst_size = secsize;
861         if (mboot.bootinst != NULL)
862                 free(mboot.bootinst);
863         if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL) {
864                 warnx("unable to allocate buffer to read fdisk "
865                       "partition table");
866                 return -1;
867         }
868         if (read_disk(0, mboot.bootinst) == -1) {
869                 warnx("can't read fdisk partition table");
870                 return -1;
871         }
872         if (le16dec(&mboot.bootinst[DOSMAGICOFFSET]) != DOSMAGIC) {
873                 warnx("invalid fdisk partition table found");
874                 /* So should we initialize things */
875                 return -1;
876         }
877         for (i = 0; i < NDOSPART; i++)
878                 dos_partition_dec(
879                     &mboot.bootinst[DOSPARTOFF + i * DOSPARTSIZE],
880                     &mboot.parts[i]);
881         return 0;
882 }
883
884 static int
885 write_s0()
886 {
887         int     sector, i;
888
889         if (iotest) {
890                 print_s0(-1);
891                 return 0;
892         }
893         for(i = 0; i < NDOSPART; i++)
894                 dos_partition_enc(&mboot.bootinst[DOSPARTOFF + i * DOSPARTSIZE],
895                     &mboot.parts[i]);
896         le16enc(&mboot.bootinst[DOSMAGICOFFSET], DOSMAGIC);
897         for(sector = 0; sector < mboot.bootinst_size / secsize; sector++) 
898                 if (write_disk(sector,
899                                &mboot.bootinst[sector * secsize]) == -1) {
900                         warn("can't write fdisk partition table");
901                         return -1;
902                 }
903         return(0);
904 }
905
906
907 static int
908 ok(const char *str)
909 {
910         printf("%s [n] ", str);
911         fflush(stdout);
912         if (fgets(lbuf, LBUF, stdin) == NULL)
913                 exit(1);
914         lbuf[strlen(lbuf)-1] = 0;
915
916         if (*lbuf &&
917                 (!strcmp(lbuf, "yes") || !strcmp(lbuf, "YES") ||
918                  !strcmp(lbuf, "y") || !strcmp(lbuf, "Y")))
919                 return 1;
920         else
921                 return 0;
922 }
923
924 static int
925 decimal(const char *str, int *num, int deflt)
926 {
927         int acc = 0, c;
928         char *cp;
929
930         while (1) {
931                 printf("Supply a decimal value for \"%s\" [%d] ", str, deflt);
932                 fflush(stdout);
933                 if (fgets(lbuf, LBUF, stdin) == NULL)
934                         exit(1);
935                 lbuf[strlen(lbuf)-1] = 0;
936
937                 if (!*lbuf)
938                         return 0;
939
940                 cp = lbuf;
941                 while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
942                 if (!c)
943                         return 0;
944                 while ((c = *cp++)) {
945                         if (c <= '9' && c >= '0')
946                                 acc = acc * 10 + c - '0';
947                         else
948                                 break;
949                 }
950                 if (c == ' ' || c == '\t')
951                         while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
952                 if (!c) {
953                         *num = acc;
954                         return 1;
955                 } else
956                         printf("%s is an invalid decimal number.  Try again.\n",
957                                 lbuf);
958         }
959
960 }
961
962 static const char *
963 get_type(int type)
964 {
965         int     numentries = (sizeof(part_types)/sizeof(struct part_type));
966         int     counter = 0;
967         struct  part_type *ptr = part_types;
968
969
970         while(counter < numentries) {
971                 if(ptr->type == type)
972                         return(ptr->name);
973                 ptr++;
974                 counter++;
975         }
976         return("unknown");
977 }
978
979
980 static void
981 parse_config_line(char *line, CMD *command)
982 {
983     char        *cp, *end;
984
985     cp = line;
986     while (1) {
987         memset(command, 0, sizeof(*command));
988
989         while (isspace(*cp)) ++cp;
990         if (*cp == '\0' || *cp == '#')
991             break;
992         command->cmd = *cp++;
993
994         /*
995          * Parse args
996          */
997             while (1) {
998             while (isspace(*cp)) ++cp;
999             if (*cp == '#')
1000                 break;          /* found comment */
1001             if (isalpha(*cp))
1002                 command->args[command->n_args].argtype = *cp++;
1003             if (!isdigit(*cp))
1004                 break;          /* assume end of line */
1005             end = NULL;
1006             command->args[command->n_args].arg_val = strtol(cp, &end, 0);
1007             if (cp == end)
1008                 break;          /* couldn't parse number */
1009             cp = end;
1010             command->n_args++;
1011         }
1012         break;
1013     }
1014 }
1015
1016
1017 static int
1018 process_geometry(CMD *command)
1019 {
1020     int         status = 1, i;
1021
1022     while (1) {
1023         geom_processed = 1;
1024             if (part_processed) {
1025             warnx(
1026         "ERROR line %d: the geometry specification line must occur before\n\
1027     all partition specifications",
1028                     current_line_number);
1029             status = 0;
1030             break;
1031         }
1032             if (command->n_args != 3) {
1033             warnx("ERROR line %d: incorrect number of geometry args",
1034                     current_line_number);
1035             status = 0;
1036             break;
1037         }
1038             dos_cyls = 0;
1039             dos_heads = 0;
1040             dos_sectors = 0;
1041             for (i = 0; i < 3; ++i) {
1042                     switch (command->args[i].argtype) {
1043             case 'c':
1044                 dos_cyls = command->args[i].arg_val;
1045                 break;
1046             case 'h':
1047                 dos_heads = command->args[i].arg_val;
1048                 break;
1049             case 's':
1050                 dos_sectors = command->args[i].arg_val;
1051                 break;
1052             default:
1053                 warnx(
1054                 "ERROR line %d: unknown geometry arg type: '%c' (0x%02x)",
1055                         current_line_number, command->args[i].argtype,
1056                         command->args[i].argtype);
1057                 status = 0;
1058                 break;
1059             }
1060         }
1061         if (status == 0)
1062             break;
1063
1064         dos_cylsecs = dos_heads * dos_sectors;
1065
1066         /*
1067          * Do sanity checks on parameter values
1068          */
1069             if (dos_cyls == 0) {
1070             warnx("ERROR line %d: number of cylinders not specified",
1071                     current_line_number);
1072             status = 0;
1073         }
1074             if (dos_cyls > 1024) {
1075             warnx(
1076         "WARNING line %d: number of cylinders (%d) may be out-of-range\n\
1077     (must be within 1-1024 for normal BIOS operation, unless the entire disk\n\
1078     is dedicated to FreeBSD)",
1079                     current_line_number, dos_cyls);
1080         }
1081
1082             if (dos_heads == 0) {
1083             warnx("ERROR line %d: number of heads not specified",
1084                     current_line_number);
1085             status = 0;
1086             } else if (dos_heads > 256) {
1087             warnx("ERROR line %d: number of heads must be within (1-256)",
1088                     current_line_number);
1089             status = 0;
1090         }
1091
1092             if (dos_sectors == 0) {
1093             warnx("ERROR line %d: number of sectors not specified",
1094                     current_line_number);
1095             status = 0;
1096             } else if (dos_sectors > 63) {
1097             warnx("ERROR line %d: number of sectors must be within (1-63)",
1098                     current_line_number);
1099             status = 0;
1100         }
1101
1102         break;
1103     }
1104     return (status);
1105 }
1106
1107
1108 static int
1109 process_partition(CMD *command)
1110 {
1111     int                         status = 0, partition;
1112     u_int32_t                   prev_head_boundary, prev_cyl_boundary;
1113     u_int32_t                   adj_size, max_end;
1114     struct dos_partition        *partp;
1115
1116         while (1) {
1117         part_processed = 1;
1118                 if (command->n_args != 4) {
1119             warnx("ERROR line %d: incorrect number of partition args",
1120                     current_line_number);
1121             break;
1122         }
1123         partition = command->args[0].arg_val;
1124                 if (partition < 1 || partition > 4) {
1125             warnx("ERROR line %d: invalid partition number %d",
1126                     current_line_number, partition);
1127             break;
1128         }
1129         partp = ((struct dos_partition *) &mboot.parts) + partition - 1;
1130         bzero((char *)partp, sizeof (struct dos_partition));
1131         partp->dp_typ = command->args[1].arg_val;
1132         partp->dp_start = command->args[2].arg_val;
1133         partp->dp_size = command->args[3].arg_val;
1134         max_end = partp->dp_start + partp->dp_size;
1135
1136                 if (partp->dp_typ == 0) {
1137             /*
1138              * Get out, the partition is marked as unused.
1139              */
1140             /*
1141              * Insure that it's unused.
1142              */
1143             bzero((char *)partp, sizeof (struct dos_partition));
1144             status = 1;
1145             break;
1146         }
1147
1148         /*
1149          * Adjust start upwards, if necessary, to fall on a head boundary.
1150          */
1151                 if (partp->dp_start % dos_sectors != 0) {
1152             prev_head_boundary = partp->dp_start / dos_sectors * dos_sectors;
1153             if (max_end < dos_sectors ||
1154                             prev_head_boundary > max_end - dos_sectors) {
1155                 /*
1156                  * Can't go past end of partition
1157                  */
1158                 warnx(
1159         "ERROR line %d: unable to adjust start of partition %d to fall on\n\
1160     a head boundary",
1161                         current_line_number, partition);
1162                 break;
1163             }
1164             warnx(
1165         "WARNING: adjusting start offset of partition %d\n\
1166     from %u to %u, to fall on a head boundary",
1167                     partition, (u_int)partp->dp_start,
1168                     (u_int)(prev_head_boundary + dos_sectors));
1169             partp->dp_start = prev_head_boundary + dos_sectors;
1170         }
1171
1172         /*
1173          * Adjust size downwards, if necessary, to fall on a cylinder
1174          * boundary.
1175          */
1176         prev_cyl_boundary =
1177             ((partp->dp_start + partp->dp_size) / dos_cylsecs) * dos_cylsecs;
1178         if (prev_cyl_boundary > partp->dp_start)
1179             adj_size = prev_cyl_boundary - partp->dp_start;
1180                 else {
1181             warnx(
1182         "ERROR: could not adjust partition to start on a head boundary\n\
1183     and end on a cylinder boundary.");
1184             return (0);
1185         }
1186                 if (adj_size != partp->dp_size) {
1187             warnx(
1188         "WARNING: adjusting size of partition %d from %u to %u\n\
1189     to end on a cylinder boundary",
1190                     partition, (u_int)partp->dp_size, (u_int)adj_size);
1191             partp->dp_size = adj_size;
1192         }
1193                 if (partp->dp_size == 0) {
1194             warnx("ERROR line %d: size of partition %d is zero",
1195                     current_line_number, partition);
1196             break;
1197         }
1198
1199         dos(partp);
1200         status = 1;
1201         break;
1202     }
1203     return (status);
1204 }
1205
1206
1207 static int
1208 process_active(CMD *command)
1209 {
1210     int                         status = 0, partition, i;
1211     struct dos_partition        *partp;
1212
1213         while (1) {
1214         active_processed = 1;
1215                 if (command->n_args != 1) {
1216             warnx("ERROR line %d: incorrect number of active args",
1217                     current_line_number);
1218             status = 0;
1219             break;
1220         }
1221         partition = command->args[0].arg_val;
1222                 if (partition < 1 || partition > 4) {
1223             warnx("ERROR line %d: invalid partition number %d",
1224                     current_line_number, partition);
1225             break;
1226         }
1227         /*
1228          * Reset active partition
1229          */
1230         partp = ((struct dos_partition *) &mboot.parts);
1231         for (i = 0; i < NDOSPART; i++)
1232             partp[i].dp_flag = 0;
1233         partp[partition-1].dp_flag = ACTIVE;
1234
1235         status = 1;
1236         break;
1237     }
1238     return (status);
1239 }
1240
1241
1242 static int
1243 process_line(char *line)
1244 {
1245     CMD         command;
1246     int         status = 1;
1247
1248         while (1) {
1249         parse_config_line(line, &command);
1250                 switch (command.cmd) {
1251         case 0:
1252             /*
1253              * Comment or blank line
1254              */
1255             break;
1256         case 'g':
1257             /*
1258              * Set geometry
1259              */
1260             status = process_geometry(&command);
1261             break;
1262         case 'p':
1263             status = process_partition(&command);
1264             break;
1265         case 'a':
1266             status = process_active(&command);
1267             break;
1268         default:
1269             status = 0;
1270             break;
1271         }
1272         break;
1273     }
1274     return (status);
1275 }
1276
1277
1278 static int
1279 read_config(char *config_file)
1280 {
1281     FILE        *fp = NULL;
1282     int         status = 1;
1283     char        buf[1010];
1284
1285         while (1) {
1286                 if (strcmp(config_file, "-") != 0) {
1287             /*
1288              * We're not reading from stdin
1289              */
1290                         if ((fp = fopen(config_file, "r")) == NULL) {
1291                 status = 0;
1292                 break;
1293             }
1294                 } else {
1295             fp = stdin;
1296         }
1297         current_line_number = 0;
1298                 while (!feof(fp)) {
1299             if (fgets(buf, sizeof(buf), fp) == NULL)
1300                 break;
1301             ++current_line_number;
1302             status = process_line(buf);
1303             if (status == 0)
1304                 break;
1305             }
1306         break;
1307     }
1308         if (fp) {
1309         /*
1310          * It doesn't matter if we're reading from stdin, as we've reached EOF
1311          */
1312         fclose(fp);
1313     }
1314     return (status);
1315 }
1316
1317
1318 static void
1319 reset_boot(void)
1320 {
1321     int                         i;
1322     struct dos_partition        *partp;
1323
1324     init_boot();
1325         for (i = 0; i < 4; ++i) {
1326         partp = ((struct dos_partition *) &mboot.parts) + i;
1327         bzero((char *)partp, sizeof (struct dos_partition));
1328     }
1329 }
1330
1331 static int
1332 sanitize_partition(struct dos_partition *partp)
1333 {
1334     u_int32_t                   prev_head_boundary, prev_cyl_boundary;
1335     u_int32_t                   max_end, size, start;
1336
1337     start = partp->dp_start;
1338     size = partp->dp_size;
1339     max_end = start + size;
1340     /* Only allow a zero size if the partition is being marked unused. */
1341     if (size == 0) {
1342         if (start == 0 && partp->dp_typ == 0)
1343             return (1);
1344         warnx("ERROR: size of partition is zero");
1345         return (0);
1346     }
1347     /* Return if no adjustment is necessary. */
1348     if (start % dos_sectors == 0 && (start + size) % dos_sectors == 0)
1349         return (1);
1350
1351     if (start == 0) {
1352             warnx("WARNING: partition overlaps with partition table");
1353             if (ok("Correct this automatically?"))
1354                     start = dos_sectors;
1355     }
1356     if (start % dos_sectors != 0)
1357         warnx("WARNING: partition does not start on a head boundary");
1358     if ((start  +size) % dos_sectors != 0)
1359         warnx("WARNING: partition does not end on a cylinder boundary");
1360     warnx("WARNING: this may confuse the BIOS or some operating systems");
1361     if (!ok("Correct this automatically?"))
1362         return (1);
1363
1364     /*
1365      * Adjust start upwards, if necessary, to fall on a head boundary.
1366      */
1367     if (start % dos_sectors != 0) {
1368         prev_head_boundary = start / dos_sectors * dos_sectors;
1369         if (max_end < dos_sectors ||
1370             prev_head_boundary >= max_end - dos_sectors) {
1371             /*
1372              * Can't go past end of partition
1373              */
1374             warnx(
1375     "ERROR: unable to adjust start of partition to fall on a head boundary");
1376             return (0);
1377         }
1378         start = prev_head_boundary + dos_sectors;
1379     }
1380
1381     /*
1382      * Adjust size downwards, if necessary, to fall on a cylinder
1383      * boundary.
1384      */
1385     prev_cyl_boundary = ((start + size) / dos_cylsecs) * dos_cylsecs;
1386     if (prev_cyl_boundary > start)
1387         size = prev_cyl_boundary - start;
1388     else {
1389         warnx("ERROR: could not adjust partition to start on a head boundary\n\
1390     and end on a cylinder boundary.");
1391         return (0);
1392     }
1393
1394     /* Finally, commit any changes to partp and return. */
1395     if (start != partp->dp_start) {
1396         warnx("WARNING: adjusting start offset of partition to %u",
1397             (u_int)start);
1398         partp->dp_start = start;
1399     }
1400     if (size != partp->dp_size) {
1401         warnx("WARNING: adjusting size of partition to %u", (u_int)size);
1402         partp->dp_size = size;
1403     }
1404
1405     return (1);
1406 }
1407
1408 /*
1409  * Try figuring out the root device's canonical disk name.
1410  * The following choices are considered:
1411  *   /dev/ad0s1a     => /dev/ad0
1412  *   /dev/da0a       => /dev/da0
1413  *   /dev/vinum/root => /dev/vinum/root
1414  */
1415 static char *
1416 get_rootdisk(void)
1417 {
1418         struct statfs rootfs;
1419         regex_t re;
1420 #define NMATCHES 2
1421         regmatch_t rm[NMATCHES];
1422         char *s;
1423         int rv;
1424
1425         if (statfs("/", &rootfs) == -1)
1426                 err(1, "statfs(\"/\")");
1427
1428         if ((rv = regcomp(&re, "^(/dev/[a-z/]+[0-9]+)([sp][0-9]+)?[a-h]?$",
1429                     REG_EXTENDED)) != 0)
1430                 errx(1, "regcomp() failed (%d)", rv);
1431         if ((rv = regexec(&re, rootfs.f_mntfromname, NMATCHES, rm, 0)) != 0)
1432                 errx(1,
1433 "mounted root fs resource doesn't match expectations (regexec returned %d)",
1434                     rv);
1435         if ((s = malloc(rm[1].rm_eo - rm[1].rm_so + 1)) == NULL)
1436                 errx(1, "out of memory");
1437         memcpy(s, rootfs.f_mntfromname + rm[1].rm_so,
1438             rm[1].rm_eo - rm[1].rm_so);
1439         s[rm[1].rm_eo - rm[1].rm_so] = 0;
1440
1441         return s;
1442 }