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 43 static void emitconv(FILE *); 44 static void emitdev(FILE *); 45 static void emitdevm(FILE *); 46 static void emitheader(FILE *); 47 48 int 49 mkdevsw(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 80 static void 81 emitheader(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 */ 92 static void 93 emitdevm(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 */ 157 static void 158 emitconv(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 struct nvlist *nv; 166 const char *d_class, *d_flags = "0"; 167 int d_vec[2] = { 0, 0 }; 168 int i = 0; 169 170 /* 171 * "parse" info. currently the rules are simple: 172 * 1) first entry defines class 173 * 2) next ones without n_str are d_vectdim 174 * 3) next one with n_str is d_flags 175 * 4) EOL 176 */ 177 nv = dm->dm_devnodes; 178 d_class = nv->nv_str; 179 while ((nv = nv->nv_next) != NULL) { 180 if (i > 2) 181 panic("invalid devnode definition"); 182 if (nv->nv_str) { 183 d_flags = nv->nv_str; 184 break; 185 } 186 d_vec[i++] = nv->nv_num; 187 } 188 189 fprintf(fp, "\t{ \"%s\", %d, %d, %s, %s, { %d, %d }},\n", 190 dm->dm_name, dm->dm_bmajor, dm->dm_cmajor, 191 d_class, d_flags, d_vec[0], d_vec[1]); 192 193 } 194 fputs("};\n\n" 195 "struct devsw_conv *devsw_conv = devsw_conv0;\n" 196 "int max_devsw_convs = __arraycount(devsw_conv0);\n", 197 fp); 198 } 199 200 /* 201 * Emit specific device major informations. 202 */ 203 static void 204 emitdev(FILE *fp) 205 { 206 struct devm *dm; 207 char mstr[16]; 208 209 fputs("\n", fp); 210 211 (void)strlcpy(mstr, "swap", sizeof(mstr)); 212 if ((dm = ht_lookup(bdevmtab, intern(mstr))) != NULL) { 213 fprintf(fp, "const dev_t swapdev = makedev(%d, 0);\n", 214 dm->dm_bmajor); 215 } 216 217 (void)strlcpy(mstr, "mem", sizeof(mstr)); 218 if ((dm = ht_lookup(cdevmtab, intern(mstr))) == NULL) 219 panic("memory device is not configured"); 220 fprintf(fp, "const dev_t zerodev = makedev(%d, DEV_ZERO);\n", 221 dm->dm_cmajor); 222 223 fputs("\n/* mem_no is only used in iskmemdev() */\n", fp); 224 fprintf(fp, "const int mem_no = %d;\n", dm->dm_cmajor); 225 } 226