summaryrefslogtreecommitdiff
path: root/sys/arch/atari/stand/tostools/chg_pid/chg_pid.c
blob: 35826e506a61a92e47817160f1edbe929501f4fc (plain)
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*	$NetBSD: chg_pid.c,v 1.9 2009/10/20 19:10:11 snj Exp $	*/

/*
 * Copyright (c) 1995 L. Weppelman
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 *
 * This program changes the partition id field (p_id) in the GEM
 * partition info. NetBSD uses these id-fields to determine the kind
 * of partition. Sensible id's to set are:
 *  NBU : NetBSD User partition
 *  NBR : NetBSD Root partition
 *  NBS : NetBSD Swap partition
 *  NBD : General NetBSD partition
 *  RAW : Partition hidden for GEMDOS
 *
 * When NetBSD auto boots, the first 'NBR' partition found when scanning the
 * SCSI-disks becomes the active root partition. The same goes for 'NBS'.
 * Drives are scanned in 'SCSI-id' order.
 */
#include <sys/types.h>
#include <osbind.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include "libtos.h"

#ifndef Dmawrite
#define	Dmawrite DMAwrite
#endif
#ifndef Dmaread
#define	Dmaread	 DMAread
#endif

/*
 * Format of GEM root sector
 */
typedef struct gem_part {
	u_char	p_flg;		/* bit 0 is in-use flag			*/
	char	p_id[3];	/* id: GEM, BGM, XGM, UNX, MIX		*/
	u_long	p_st;		/* block where partition starts		*/
	u_long	p_size;		/* partition size			*/
} GEM_PART;

/*
 * Defines for p_flg
 */
#define	P_VALID		0x01	/* info is valid			*/
#define	P_ACTIVE	0x80	/* partition is active			*/

#define	NGEM_PARTS	4	/* Max. partition infos in root sector	*/

typedef struct gem_root {
	u_char	    fill[0x1c2];	/* Filler, can be boot code	*/
	u_long	    hd_siz;		/* size of entire volume	*/
	GEM_PART    parts[NGEM_PARTS];	/* see above			*/
	u_long	    bsl_st;		/* start of bad-sector list	*/
	u_long	    bsl_cnt;	  	/* nr. blocks in bad-sector list*/
	u_short	    csum;		/* checksum	correction	*/
} GEM_ROOT;

void	help 		PROTO((void));
void	usage		PROTO((void));
int	chg_tosparts	PROTO((int, int, char *));
void	change_it	PROTO((int, GEM_PART *, char *));
int	read_block	PROTO((void *, int, int));
int	write_block	PROTO((void *, int, int));
void	set_csum	PROTO((char *));

const char version[] = "$Revision: 1.9 $";

char	*Progname = NULL;		/* What are we called		*/
int	t_flag    = 0;			/* Test -- don't actually do it	*/
int	v_flag    = 0;			/* show version			*/
int	h_flag    = 0;			/* show help			*/

int
main(int argc, char *argv[])
{
	/*
	 * Option parsing
	 */
	extern	int	optind;
	extern	char	*optarg;

	int	driveno  = 0;
	int	partno   = 0;
	char	*newname = NULL;
	int	c;

	init_toslib(argv[0]);
	Progname = argv[0];

	while ((c = getopt(argc, argv, "htVwo:")) != -1) {
		switch (c) {
			case 'h':
				h_flag = 1;
				break;
			case 'o':
				redirect_output(optarg);
				break;
			case 't':
				t_flag = 1;
				break;
			case 'V':
				v_flag = 1;
				break;
			case 'w':
				set_wait_for_key();
				break;
			default:
				usage();
		}
	}
	argc -= optind;
	argv += optind;

	if (h_flag)
		help();

	if (v_flag) {
		eprintf("%s\r\n", version);
		if (argc != 3)
			xexit(0);
	}

	if (argc != 3)
		usage();

	eprintf("Note: >>> Both drive and partition numbers start "
		"at 0! <<<\r\n");
			
	driveno = atoi(argv[0]);
	partno  = atoi(argv[1]);
	newname = argv[2];
	eprintf("About to change id of partition %d on drive %d to %s\r\n",
						partno, driveno, newname);

	if (!t_flag)
		c = key_wait("Are you sure (y/n)? ");
	else c = 'y';
	switch(c) {
		case 'y':
		case 'Y':
			if(chg_tosparts(partno, driveno, newname)) {
				if (!t_flag)
					eprintf("Done\r\n");
				else eprintf("Not Done\r\n");
				xexit(0);
			}
			else eprintf("Partition number not found\r\n");
			break;
		default :
			eprintf("Aborted\r\n");
			xexit(1);
			break;
	}
	xexit(0);
}

int chg_tosparts(chg_part, drive, newname)
int	chg_part, drive;
char	*newname;
{
    GEM_ROOT	*g_root;
    GEM_PART	g_local[NGEM_PARTS];
    char	buf[512];
    int		pno  = 0;
    int		i;

    /*
     * Read root sector
     */
    if (read_block(buf, 0, drive) == 0)
	fatal(-1, "Cannot read block 0\r\n");

    /*
     * Make local copy of partition info, we may need to re-use
     * the buffer in case of 'XGM' partitions.
     */
    g_root  = (GEM_ROOT*)buf;
    memcpy(g_local, g_root->parts, NGEM_PARTS*sizeof(GEM_PART));

    for (i = 0; i < NGEM_PARTS; i++) {
	if (!(g_local[i].p_flg & 1)) 
	    continue;
	if (!strncmp(g_local[i].p_id, "XGM", 3)) {
	    int	j;
	    daddr_t	new_root = g_local[i].p_st;

	    /*
	     * Loop through extended partition list
	     */
	    for(;;) {
		if (read_block(buf, new_root, drive) == 0)
		    fatal(-1, "Cannot read block %d\r\n", new_root);
		for (j = 0; j < NGEM_PARTS; j++) {
		    if (!(g_root->parts[j].p_flg & 1))
			continue;
		    if (!strncmp(g_root->parts[j].p_id, "XGM", 3)) {
			new_root = g_local[i].p_st + g_root->parts[j].p_st;
			break;
		    }
		    else {
			if (pno == chg_part) {
			    change_it(pno, &g_root->parts[j], newname);
			    if (t_flag)
				return(1);
			    if (write_block(buf, new_root, drive) == 0)
				fatal(-1, "Cannot write block %d\r\n",new_root);
			    return(1);
			}
			pno++;
		    }
		}
		if (j == NGEM_PARTS)
		    break;
	    }
	}
	else {
	    if (pno == chg_part) {
		/*
		 * Re-read block 0
		 */
		if (read_block(buf, 0, drive) == 0)
		    fatal(-1, "Cannot read block 0\r\n");
		change_it(pno, &g_root->parts[i], newname);
		if (t_flag)
		    return(1);
		set_csum(buf);
		if (write_block(buf, 0, drive) == 0)
		    fatal(-1, "Cannot write block 0\r\n");
		return(1);
	    }
	    pno++;
	}
    }
    return(0);
}

void change_it(pno, gp, newname)
int		pno;
GEM_PART	*gp;
char		*newname;
{
	char	s1[4], s2[4];

	strncpy(s1, gp->p_id, 3);
	strncpy(s2, newname, 3);
	s1[3] = s2[3] = '\0';
	eprintf("Changing partition %d: %s -> %s ...", pno, s1, s2);
	gp->p_id[0] = s2[0]; gp->p_id[1] = s2[1]; gp->p_id[2] = s2[2];
}

int read_block(buf, blkno, drive)
void	*buf;
int	blkno;
int	drive;
{
	if(Dmaread(blkno, 1, buf, drive + 8) != 0)
		return(0);
	return(1);
}

int write_block(buf, blkno, drive)
void	*buf;
int	blkno;
int	drive;
{
	if(Dmawrite(blkno, 1, buf, drive + 8) != 0)
		return(0);
	return(1);
}

void set_csum(buf)
char	*buf;
{
	unsigned short	*p = (unsigned short *)buf;
	unsigned short	csum = 0;
	int		i;

	p[255] = 0;
	for(i = 0; i < 256; i++)
		csum += *p++;
	*--p = (0x1234 - csum) & 0xffff;
}

void usage(void)
{
	eprintf("Usage: %s [-hVwt] [ -o <output file>] <driveno> <partno> "
		    "<newid>\r\n", Progname);
	xexit(1);
}

void
help(void)
{
	eprintf("\r
Change partition identifiers\r
\r
Usage: %s [-hVwt] [ -o <output file>] <driveno> <partno> <newid>\r
\r
Description of options:\r
\r
\t-h  What you're getting right now.\r
\t-o  Write output to both <output file> and stdout.\r
\t-V  Print program version.\r
\t-w  Wait for a keypress before exiting.\r
\t-t  Test mode. It does everyting except the modifications on disk.\r
\r
The <driveno> and <partno> arguments specify the drive and the partition\r
this program acts on. Both are zero based.\r
The <newid> argument specifies a 3 letter string that will become the new\r
partition-id.\r
Finally note that the actions of %s are reversable.\r
", Progname, Progname);
	xexit(0);
}