2 * Copyright (c) 1996 Whistle Communications
5 * Permission to use, copy, modify and distribute this software and its
6 * documentation is hereby granted, provided that both the copyright
7 * notice and this permission notice appear in all copies of the
8 * software, derivative works or modified versions, and any portions
9 * thereof, and that both notices appear in supporting documentation.
11 * Whistle Communications allows free use of this software in its "as is"
12 * condition. Whistle Communications disclaims any liability of any kind for
13 * any damages whatsoever resulting from the use of this software.
15 * $FreeBSD: src/sbin/i386/nextboot/nextboot.c,v 1.6 1999/08/28 00:13:06 peter Exp $
16 * $DragonFly: src/sbin/i386/nextboot/nextboot.c,v 1.2 2003/06/17 04:27:33 dillon Exp $
19 #include <sys/types.h>
20 #include <sys/disklabel.h>
29 unsigned char padding[2]; /* force the longs to be long aligned */
30 unsigned char bootinst[DOSPARTOFF];
31 struct dos_partition parts[4];
32 unsigned short int signature;
36 #define NAMEBLOCK 1 /* 2nd block */
38 #define ENABLE_MAGIC 0xfadefeed
39 #define DISABLE_MAGIC 0xfadefade
44 #define BOOT_MAGIC 0xAA55
48 fprintf (stderr, "%s\n%s\n",
49 "usage: nextboot [-b] device bootstring [bootstring] ...",
50 " nextboot {-e,-d} device");
55 main (int argc, char** argv)
58 char namebuf[1024], *cp = namebuf;
64 while ((ch = getopt(argc, argv, "bde")) != -1) {
83 if ( (dflag + eflag + bflag) > 1 ) {
95 if ((fd = open(argv[0], O_RDWR, 0)) < 0)
96 errx(1, "can't open %s", argv[0]);
101 /*******************************************
102 * Check that we have an MBR
104 if (lseek(fd,0,0) == -1)
106 if (read (fd,&mboot.bootinst[0],BLOCKSIZE ) != BLOCKSIZE)
108 if (mboot.signature != (unsigned short)BOOT_MAGIC)
109 errx(1, "no fdisk part.. not touching block 1");
111 /*******************************************
112 * And check that none of the partitions in it cover the name block;
114 for ( part = 0; part < 4; part++) {
115 if( mboot.parts[part].dp_size
116 && (mboot.parts[part].dp_start <= NAMEBLOCK)
117 && (mboot.parts[part].dp_start
118 + mboot.parts[part].dp_size > NAMEBLOCK))
120 "name sector lies within a Bios partition: aborting write");
124 /*******************************************
125 * Now check the name sector itself to see if it's been initialized.
127 if (lseek(fd,NAMEBLOCK * BLOCKSIZE,0) == -1)
129 if (read(fd,namebuf,BLOCKSIZE) != BLOCKSIZE)
131 /*******************************************
132 * check if we are just enabling or disabling
133 * Remember the flags are exclusive..
135 if(!bflag) { /* don't care what's there if bflag is set */
136 switch(*(unsigned long *)cp)
142 errx(1, "namesector not initialized, use the -b flag");
147 /*******************************************
148 * If the z or r flag is set, damage or restore the magic number..
149 * to disable/enable the feature
152 *(unsigned long *)cp = DISABLE_MAGIC;
154 *(unsigned long *)cp = ENABLE_MAGIC;
156 if ((!dflag) && (!eflag)) {
157 /*******************************************
158 * Create a new namesector in ram
161 for ( i = 0 ; i < argc ; i++ ) {
165 strncpy(cp,argv[i],j);
172 namebuf[BLOCKSIZE-1] = 0; /* paranoid */
173 namebuf[BLOCKSIZE] = 0xff;
176 /*******************************************
179 if (lseek(fd,NAMEBLOCK * BLOCKSIZE,0) == -1)
181 if(write (fd,namebuf,BLOCKSIZE ) != BLOCKSIZE)
185 /*******************************************
186 * just to be safe/paranoid.. read it back..
189 if (lseek(fd,NAMEBLOCK * BLOCKSIZE,0) == -1)
190 err(1, "lseek (second)");
191 read (fd,namebuf,512);
192 for (i = 0;i< 16;i++) {
193 for ( j = 0; j < 16; j++) {
194 printf("%02x ",(unsigned char )namebuf[(i*16) + j ]);