1 /*	$NetBSD: writedtab.c,v 1.5 2009/03/14 21:04:06 dsl 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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include "privahdi.h"
33 #include <disktab.h>
34 #include <fcntl.h>
35 #include <stdio.h>
36 #include <string.h>
37 
38 /*
39  * Write AHDI partitions to disk
40  */
41 int
42 ahdi_writedisktab (struct ahdi_ptable *ptable, char *disktype, char *disktab, char *dtype)
43 {
44 	FILE			*fd;
45 	int			 pid, i, j, todo;
46 
47 	if ((fd = fopen (strlen (disktab) ? disktab : _PATH_DISKTAB, "a"))
48 	    == NULL)
49 		return (-1);
50 
51 	fprintf (fd, "%s disk|%s:\\\n",
52 	    strlen (dtype) ? dtype : "SCSI", disktype);
53 	fprintf (fd, "\t:ty#winchester:dt=%s:ns#%u:nt#%u:nc#%u:sc#%u:su#%u",
54 	    strlen (dtype) ? dtype : "SCSI", ptable->nsectors,
55 	    ptable->ntracks, ptable->ncylinders, ptable->secpercyl,
56 	    ptable->secperunit);
57 
58 	todo = ptable->nparts;
59 	j = 0;
60 	while (todo) {
61 		for (i = 0; i < ptable->nparts; i++) {
62 			if (j == RAW_PART) {
63 				fprintf (fd,
64 				    "\\\n\t:p%c#%u:o%c#0:t%c=unknown:",
65 				    RAW_PART + 'a', ptable->secperunit,
66 				    RAW_PART + 'a', RAW_PART + 'a');
67 				break;
68 			}
69 			if (ptable->parts[i].letter == j) {
70 				fprintf (fd, "\\\n\t:p%c#%u:o%c#%u:t%c=",
71 				    ptable->parts[i].letter + 'a',
72 				    ptable->parts[i].size,
73 				    ptable->parts[i].letter + 'a',
74 				    ptable->parts[i].start,
75 				    ptable->parts[i].letter + 'a');
76 				pid = AHDI_MKPID (ptable->parts[i].id[0],
77 				    ptable->parts[i].id[1],
78 				    ptable->parts[i].id[2]);
79 				switch (pid) {
80 				case AHDI_PID_NBD:
81 					fprintf (fd, "4.2BSD:");
82 					break;
83 				case AHDI_PID_SWP:
84 					fprintf (fd, "swap:");
85 					break;
86 				case AHDI_PID_GEM:
87 				case AHDI_PID_BGM:
88 					fprintf (fd, "MSDOS:");
89 					break;
90 				default:
91 					fprintf (fd, "unknown:" );
92 				}
93 				todo--;
94 				break;
95 			}
96 		}
97 		j++;
98 	}
99 	if (j <= RAW_PART) {
100 		fprintf (fd, "\\\n\t:p%c#%u:o%c#0:t%c=unknown:",
101 		    RAW_PART + 'a', ptable->secperunit,
102 		    RAW_PART + 'a', RAW_PART + 'a');
103 	}
104 	fprintf (fd, "\n\n");
105 
106 	fclose (fd);
107 	return (1);
108 }
109