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.5 2007/05/20 23:21:36 dillon Exp $
19 #include <sys/types.h>
20 #include <sys/diskmbr.h>
30 unsigned char padding[2]; /* force the longs to be long aligned */
31 unsigned char bootinst[DOSPARTOFF];
32 struct dos_partition parts[4];
33 unsigned short int signature;
37 #define NAMEBLOCK 1 /* 2nd block */
39 #define ENABLE_MAGIC 0xfadefeed
40 #define DISABLE_MAGIC 0xfadefade
45 #define BOOT_MAGIC 0xAA55
49 fprintf (stderr, "%s\n%s\n",
50 "usage: nextboot [-b] device bootstring [bootstring] ...",
51 " nextboot {-e,-d} device");
56 main (int argc, char** argv)
59 char namebuf[1024], *cp = namebuf;
65 while ((ch = getopt(argc, argv, "bde")) != -1) {
84 if ( (dflag + eflag + bflag) > 1 ) {
96 if ((fd = open(argv[0], O_RDWR, 0)) < 0)
97 errx(1, "can't open %s", argv[0]);
102 /*******************************************
103 * Check that we have an MBR
105 if (lseek(fd,0,0) == -1)
107 if (read (fd,&mboot.bootinst[0],BLOCKSIZE ) != BLOCKSIZE)
109 if (mboot.signature != (unsigned short)BOOT_MAGIC)
110 errx(1, "no fdisk part.. not touching block 1");
112 /*******************************************
113 * And check that none of the partitions in it cover the name block;
115 for ( part = 0; part < 4; part++) {
116 if( mboot.parts[part].dp_size
117 && (mboot.parts[part].dp_start <= NAMEBLOCK)
118 && (mboot.parts[part].dp_start
119 + mboot.parts[part].dp_size > NAMEBLOCK))
121 "name sector lies within a Bios partition: aborting write");
125 /*******************************************
126 * Now check the name sector itself to see if it's been initialized.
128 if (lseek(fd,NAMEBLOCK * BLOCKSIZE,0) == -1)
130 if (read(fd,namebuf,BLOCKSIZE) != BLOCKSIZE)
132 /*******************************************
133 * check if we are just enabling or disabling
134 * Remember the flags are exclusive..
136 if(!bflag) { /* don't care what's there if bflag is set */
137 switch(*(unsigned long *)cp)
143 errx(1, "namesector not initialized, use the -b flag");
148 /*******************************************
149 * If the z or r flag is set, damage or restore the magic number..
150 * to disable/enable the feature
153 *(unsigned long *)cp = DISABLE_MAGIC;
155 *(unsigned long *)cp = ENABLE_MAGIC;
157 if ((!dflag) && (!eflag)) {
158 /*******************************************
159 * Create a new namesector in ram
162 for ( i = 0 ; i < argc ; i++ ) {
166 strncpy(cp,argv[i],j);
173 namebuf[BLOCKSIZE-1] = 0; /* paranoid */
174 namebuf[BLOCKSIZE] = 0xff;
177 /*******************************************
180 if (lseek(fd,NAMEBLOCK * BLOCKSIZE,0) == -1)
182 if(write (fd,namebuf,BLOCKSIZE ) != BLOCKSIZE)
186 /*******************************************
187 * just to be safe/paranoid.. read it back..
190 if (lseek(fd,NAMEBLOCK * BLOCKSIZE,0) == -1)
191 err(1, "lseek (second)");
192 read (fd,namebuf,512);
193 for (i = 0;i< 16;i++) {
194 for ( j = 0; j < 16; j++) {
195 printf("%02x ",(unsigned char )namebuf[(i*16) + j ]);