1/* $NetBSD: mkdevsw.c,v 1.9 2010/04/30 20:47:18 pooka Exp $ */ 2 3/* 4 * Copyright (c) 2002 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by MAEKAWA Masahide (gehenna@NetBSD.org). 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#if HAVE_NBTOOL_CONFIG_H 33#include "nbtool_config.h" 34#endif 35 36#include <stdio.h> 37#include <string.h> 38#include <errno.h> 39#include <err.h> 40 41#include "defs.h" 42 43static void emitconv(FILE *); 44static void emitdev(FILE *); 45static void emitdevm(FILE *); 46static void emitheader(FILE *); 47 48int 49mkdevsw(void) 50{ 51 FILE *fp; 52 53 if ((fp = fopen("devsw.c.tmp", "w")) == NULL) { 54 warn("cannot create devsw.c"); 55 return (1); 56 } 57 58 emitheader(fp); 59 emitdevm(fp); 60 emitconv(fp); 61 emitdev(fp); 62 63 fflush(fp); 64 if (ferror(fp)) { 65 warn("error writing devsw.c"); 66 fclose(fp); 67 return 1; 68 } 69 70 (void)fclose(fp); 71 72 if (moveifchanged("devsw.c.tmp", "devsw.c") != 0) { 73 warn("error renaming devsw.c"); 74 return (1); 75 } 76 77 return (0); 78} 79 80static void 81emitheader(FILE *fp) 82{ 83 autogen_comment(fp, "devsw.c"); 84 85 fputs("#include <sys/param.h>\n" 86 "#include <sys/conf.h>\n", fp); 87} 88 89/* 90 * Emit device switch table for character/block device. 91 */ 92static void 93emitdevm(FILE *fp) 94{ 95 struct devm *dm; 96 char mstr[16]; 97 devmajor_t i; 98 99 fputs("\n/* device switch table for block device */\n", fp); 100 101 for (i = 0 ; i <= maxbdevm ; i++) { 102 (void)snprintf(mstr, sizeof(mstr), "%d", i); 103 if ((dm = ht_lookup(bdevmtab, intern(mstr))) == NULL) 104 continue; 105 106 fprintf(fp, "extern const struct bdevsw %s_bdevsw;\n", 107 dm->dm_name); 108 } 109 110 fputs("\nconst struct bdevsw *bdevsw0[] = {\n", fp); 111 112 for (i = 0 ; i <= maxbdevm ; i++) { 113 (void)snprintf(mstr, sizeof(mstr), "%d", i); 114 if ((dm = ht_lookup(bdevmtab, intern(mstr))) == NULL) { 115 fprintf(fp, "\tNULL,\n"); 116 } else { 117 fprintf(fp, "\t&%s_bdevsw,\n", dm->dm_name); 118 } 119 } 120 121 fputs("};\n\nconst struct bdevsw **bdevsw = bdevsw0;\n", fp); 122 123 fputs("const int sys_bdevsws = __arraycount(bdevsw0);\n" 124 "int max_bdevsws = __arraycount(bdevsw0);\n", fp); 125 126 fputs("\n/* device switch table for character device */\n", fp); 127 128 for (i = 0 ; i <= maxcdevm ; i++) { 129 (void)snprintf(mstr, sizeof(mstr), "%d", i); 130 if ((dm = ht_lookup(cdevmtab, intern(mstr))) == NULL) 131 continue; 132 133 fprintf(fp, "extern const struct cdevsw %s_cdevsw;\n", 134 dm->dm_name); 135 } 136 137 fputs("\nconst struct cdevsw *cdevsw0[] = {\n", fp); 138 139 for (i = 0 ; i <= maxcdevm ; i++) { 140 (void)snprintf(mstr, sizeof(mstr), "%d", i); 141 if ((dm = ht_lookup(cdevmtab, intern(mstr))) == NULL) { 142 fprintf(fp, "\tNULL,\n"); 143 } else { 144 fprintf(fp, "\t&%s_cdevsw,\n", dm->dm_name); 145 } 146 } 147 148 fputs("};\n\nconst struct cdevsw **cdevsw = cdevsw0;\n", fp); 149 150 fputs("const int sys_cdevsws = __arraycount(cdevsw0);\n" 151 "int max_cdevsws = __arraycount(cdevsw0);\n", fp); 152} 153 154/* 155 * Emit device major conversion table. 156 */ 157static void 158emitconv(FILE *fp) 159{ 160 struct devm *dm; 161 162 fputs("\n/* device conversion table */\n" 163 "struct devsw_conv devsw_conv0[] = {\n", fp); 164 TAILQ_FOREACH(dm, &alldevms, dm_next) { 165 if (version < 20100430) { 166 /* Emit compatible structure */ 167 fprintf(fp, "\t{ \"%s\", %d, %d },\n", dm->dm_name, 168 dm->dm_bmajor, dm->dm_cmajor); 169 continue; 170 } 171 struct nvlist *nv; 172 const char *d_class, *d_flags = "0"; 173 int d_vec[2] = { 0, 0 }; 174 int i = 0; 175 176 /* 177 * "parse" info. currently the rules are simple: 178 * 1) first entry defines class 179 * 2) next ones without n_str are d_vectdim 180 * 3) next one with n_str is d_flags 181 * 4) EOL 182 */ 183 nv = dm->dm_devnodes; 184 d_class = nv->nv_str; 185 while ((nv = nv->nv_next) != NULL) { 186 if (i > 2) 187 panic("invalid devnode definition"); 188 if (nv->nv_str) { 189 d_flags = nv->nv_str; 190 break; 191 } 192 d_vec[i++] = nv->nv_num; 193 } 194 195 fprintf(fp, "\t{ \"%s\", %d, %d, %s, %s, { %d, %d }},\n", 196 dm->dm_name, dm->dm_bmajor, dm->dm_cmajor, 197 d_class, d_flags, d_vec[0], d_vec[1]); 198 199 } 200 fputs("};\n\n" 201 "struct devsw_conv *devsw_conv = devsw_conv0;\n" 202 "int max_devsw_convs = __arraycount(devsw_conv0);\n", 203 fp); 204} 205 206/* 207 * Emit specific device major informations. 208 */ 209static void 210emitdev(FILE *fp) 211{ 212 struct devm *dm; 213 char mstr[16]; 214 215 fputs("\n", fp); 216 217 (void)strlcpy(mstr, "swap", sizeof(mstr)); 218 if ((dm = ht_lookup(bdevmtab, intern(mstr))) != NULL) { 219 fprintf(fp, "const dev_t swapdev = makedev(%d, 0);\n", 220 dm->dm_bmajor); 221 } 222 223 (void)strlcpy(mstr, "mem", sizeof(mstr)); 224 if ((dm = ht_lookup(cdevmtab, intern(mstr))) == NULL) 225 panic("memory device is not configured"); 226 fprintf(fp, "const dev_t zerodev = makedev(%d, DEV_ZERO);\n", 227 dm->dm_cmajor); 228 229 fputs("\n/* mem_no is only used in iskmemdev() */\n", fp); 230 fprintf(fp, "const int mem_no = %d;\n", dm->dm_cmajor); 231} 232