1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
/*
* Copyright (c) 1996 Whistle Communications
* All Rights Reserved.
*
* Permission to use, copy, modify and distribute this software and its
* documentation is hereby granted, provided that both the copyright
* notice and this permission notice appear in all copies of the
* software, derivative works or modified versions, and any portions
* thereof, and that both notices appear in supporting documentation.
*
* Whistle Communications allows free use of this software in its "as is"
* condition. Whistle Communications disclaims any liability of any kind for
* any damages whatsoever resulting from the use of this software.
*/
#include <sys/types.h>
#include <sys/disklabel.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <unistd.h>
struct mboot
{
unsigned char padding[2]; /* force the longs to be long alligned */
unsigned char bootinst[DOSPARTOFF];
struct dos_partition parts[4];
unsigned short int signature;
};
struct mboot mboot;
#define NAMEBLOCK 1 /* 2nd block */
#define BLOCKSIZE 512
#define ENABLE_MAGIC 0xfadefeed
#define DISABLE_MAGIC 0xfadefade
static int bflag;
static int eflag;
static int dflag;
static int nameblock = NAMEBLOCK;
char * myname;
#define BOOT_MAGIC 0xAA55
static void usage(void) {
printf (" usage: %s [-b] device bootstring [bootstring] ...\n"
,myname);
printf (" or: %s {-e,-d} device \n"
,myname);
printf (" flags are mutually exclusive\n");
exit(1);
}
main (int argc, char** argv)
{
int fd = -1;
char namebuf[1024], *cp = namebuf;
int i,j;
int ch;
int part;
bflag = 0;
myname = argv[0];
while ((ch = getopt(argc, argv, "bde")) != EOF) {
switch(ch) {
case 'b':
bflag = 1;
break;
case 'd':
dflag = 1;
break;
case 'e':
eflag = 1;
break;
case '?':
default:
usage();
}
}
argc -= optind;
argv += optind;
if ( (dflag + eflag + bflag) > 1 ) {
usage();
}
if (dflag + eflag){
if(argc != 1 ) {
usage();
}
} else {
if (argc <2) {
usage();
}
}
if ((fd = open(argv[0], O_RDWR, 0)) < 0) {
perror("open");
printf ("file: %s\n",argv[0]);
usage();
}
argc--;
argv++;
/*******************************************
* Check that we have an MBR
*/
if (lseek(fd,0,0) == -1) {
perror("lseek");
exit(1);
}
if (read (fd,&mboot.bootinst[0],BLOCKSIZE ) != BLOCKSIZE) {
perror("read0");
exit(1);
}
if(mboot.signature != (unsigned short)BOOT_MAGIC) {
printf(" no fdisk part.. not touching block 1\n");
exit(1);
}
/*******************************************
* And check that none of the partitions in it cover the name block;
*/
for ( part = 0; part < 4; part++) {
if( mboot.parts[part].dp_size
&& (mboot.parts[part].dp_start <= NAMEBLOCK)
&& (mboot.parts[part].dp_start
+ mboot.parts[part].dp_size > NAMEBLOCK)) {
printf(" Name sector lies within a Bios partition.\n");
printf(" Aborting write.\n");
exit(1);
}
}
/*******************************************
* Now check the name sector itself to see if it's been initialised.
*/
if (lseek(fd,NAMEBLOCK * BLOCKSIZE,0) == -1) {
perror("lseek");
exit(1);
}
if ( read (fd,namebuf,BLOCKSIZE ) != BLOCKSIZE) {
perror("read1");
exit(1);
}
/*******************************************
* check if we are just enabling or disabling
* Remember the flags are exlusive..
*/
if(!bflag) { /* don't care what's there if bflag is set */
switch(*(unsigned long *)cp)
{
case DISABLE_MAGIC:
case ENABLE_MAGIC:
break;
default:
fprintf(stderr,
"namesector not initialised.."
"use the -b flag..\n");
exit(1);
}
}
/*******************************************
* If the z or r flag is set, damage or restore the magic number..
* to disable/enable the feature
*/
if(dflag) {
*(unsigned long *)cp = DISABLE_MAGIC;
} else {
*(unsigned long *)cp = ENABLE_MAGIC;
}
if ((!dflag) && (!eflag)) {
/*******************************************
* Create a new namesector in ram
*/
cp += 4;
for ( i = 0 ; i < argc ; i++ ) {
*cp++ = 'D';
*cp++ = 'N';
j = strlen(argv[i]);
strncpy(cp,argv[i],j);
cp += j;
*cp++ = 0;
}
*cp++ = 0xff;
*cp++ = 0xff;
*cp++ = 0xff;
namebuf[BLOCKSIZE-1] = 0; /* paranoid */
namebuf[BLOCKSIZE] = 0xff;
}
/*******************************************
* write it to disk.
*/
if (lseek(fd,NAMEBLOCK * BLOCKSIZE,0) == -1) {
perror("lseek");
exit(1);
}
if(write (fd,namebuf,BLOCKSIZE ) != BLOCKSIZE) {
perror("write");
exit(1);
}
#if 0
/*******************************************
* just to be safe/paranoid.. read it back..
* and print it..
*/
if (lseek(fd,NAMEBLOCK * BLOCKSIZE,0) == -1) {
perror("lseek (second) ");
exit(1);
}
read (fd,namebuf,512);
for (i = 0;i< 16;i++) {
for ( j = 0; j < 16; j++) {
printf("%02x ",(unsigned char )namebuf[(i*16) + j ]);
}
printf("\n");
}
#endif
exit(0);
}
|