xref: /netbsd-src/sys/arch/atari/stand/ahdilabel/writedtab.c (revision e5548b402ae4c44fb816de42c7bba9581ce23ef5)
1 /*	$NetBSD: writedtab.c,v 1.3 2005/12/11 12:17:00 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Julian Coleman, Waldi Ravens and Leo Weppelman.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include "privahdi.h"
40 #include <disktab.h>
41 #include <fcntl.h>
42 #include <stdio.h>
43 #include <string.h>
44 
45 /*
46  * Write AHDI partitions to disk
47  */
48 int
49 ahdi_writedisktab (ptable, disktype, disktab, dtype)
50 	struct ahdi_ptable	*ptable;
51 	char			*disktype, *disktab, *dtype;
52 {
53 	FILE			*fd;
54 	int			 pid, i, j, todo;
55 
56 	if ((fd = fopen (strlen (disktab) ? disktab : _PATH_DISKTAB, "a"))
57 	    == NULL)
58 		return (-1);
59 
60 	fprintf (fd, "%s disk|%s:\\\n",
61 	    strlen (dtype) ? dtype : "SCSI", disktype);
62 	fprintf (fd, "\t:ty#winchester:dt=%s:ns#%u:nt#%u:nc#%u:sc#%u:su#%u",
63 	    strlen (dtype) ? dtype : "SCSI", ptable->nsectors,
64 	    ptable->ntracks, ptable->ncylinders, ptable->secpercyl,
65 	    ptable->secperunit);
66 
67 	todo = ptable->nparts;
68 	j = 0;
69 	while (todo) {
70 		for (i = 0; i < ptable->nparts; i++) {
71 			if (j == RAW_PART) {
72 				fprintf (fd,
73 				    "\\\n\t:p%c#%u:o%c#0:t%c=unknown:",
74 				    RAW_PART + 'a', ptable->secperunit,
75 				    RAW_PART + 'a', RAW_PART + 'a');
76 				break;
77 			}
78 			if (ptable->parts[i].letter == j) {
79 				fprintf (fd, "\\\n\t:p%c#%u:o%c#%u:t%c=",
80 				    ptable->parts[i].letter + 'a',
81 				    ptable->parts[i].size,
82 				    ptable->parts[i].letter + 'a',
83 				    ptable->parts[i].start,
84 				    ptable->parts[i].letter + 'a');
85 				pid = AHDI_MKPID (ptable->parts[i].id[0],
86 				    ptable->parts[i].id[1],
87 				    ptable->parts[i].id[2]);
88 				switch (pid) {
89 				case AHDI_PID_NBD:
90 					fprintf (fd, "4.2BSD:");
91 					break;
92 				case AHDI_PID_SWP:
93 					fprintf (fd, "swap:");
94 					break;
95 				case AHDI_PID_GEM:
96 				case AHDI_PID_BGM:
97 					fprintf (fd, "MSDOS:");
98 					break;
99 				default:
100 					fprintf (fd, "unknown:" );
101 				}
102 				todo--;
103 				break;
104 			}
105 		}
106 		j++;
107 	}
108 	if (j <= RAW_PART) {
109 		fprintf (fd, "\\\n\t:p%c#%u:o%c#0:t%c=unknown:",
110 		    RAW_PART + 'a', ptable->secperunit,
111 		    RAW_PART + 'a', RAW_PART + 'a');
112 	}
113 	fprintf (fd, "\n\n");
114 
115 	fclose (fd);
116 	return (1);
117 }
118