|
|
1.1 root 1: /*
2: * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3: *
4: * @APPLE_LICENSE_HEADER_START@
5: *
6: * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7: * Reserved. This file contains Original Code and/or Modifications of
8: * Original Code as defined in and that are subject to the Apple Public
9: * Source License Version 1.1 (the "License"). You may not use this file
10: * except in compliance with the License. Please obtain a copy of the
11: * License at http://www.apple.com/publicsource and read it before using
12: * this file.
13: *
14: * The Original Code and all software distributed under the License are
15: * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16: * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17: * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18: * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19: * License for the specific language governing rights and limitations
20: * under the License.
21: *
22: * @APPLE_LICENSE_HEADER_END@
23: */
24: /* Copyright (c) 1992 by NeXT Computer, Inc.
25: *
26: * File: gendisk.c - Generate offsets for disk-resident structs.
27: *
28: * HISTORY
29: * 28-Mar-92 Doug Mitchell at NeXT
30: * Created.
31: *
32: * NeXT disks are native to the m68k architecture. When writing or reading
33: * structs to or from disks on other architectures, m68k alignment and
34: * byte ordering must be preserved. This program generates raw byte offsets
35: * from an m68k point of view for the following types:
36: *
37: * disk_label_t
38: * disktab_t
39: * partition_t
40: *
41: * This program must run on an m68k machine.
42: */
43:
44: #import <stdio.h>
45: #import <ctype.h>
46: #import <libc.h>
47: #import <bsd/dev/disk.h>
48:
49: #ifndef MACRO_BEGIN
50: # define MACRO_BEGIN do {
51: #endif MACRO_BEGIN
52:
53: #ifndef MACRO_END
54: # define MACRO_END } while (0)
55: #endif MACRO_END
56:
57: #ifndef MACRO_RETURN
58: # define MACRO_RETURN if (1) return
59: #endif MACRO_RETURN
60:
61: #define NAME_LEN 30
62:
63: #define PRINT_OFFSET(ptr_type, field) \
64: MACRO_BEGIN \
65: print_define(#ptr_type, #field); \
66: printf("%#010x\n", (& (((ptr_type)0)->field))); \
67: MACRO_END
68:
69: #define PRINT_BIT_FIELD(reg_type, field) \
70: MACRO_BEGIN \
71: reg_type __reg; \
72: __reg.field = (typeof (__reg.field)) -1; \
73: print_define(#reg_type, #field); \
74: printf("%#010x\n", CONTENTS(__reg)); \
75: MACRO_END
76:
77: #define PRINT_FIELD_INFO(reg_type, field) \
78: MACRO_BEGIN \
79: reg_type __reg; \
80: CONTENTS(__reg) = 0; \
81: __reg.field = -1; \
82: print_define(#reg_type, #field "_OFF"); \
83: printf("%d\n", bit_num(#reg_type, #field, CONTENTS(__reg))); \
84: print_define(#reg_type, #field "_WIDTH"); \
85: printf("%d\n", field_width(#reg_type, #field, CONTENTS(__reg)));\
86: MACRO_END
87:
88: #define PRINT_ENUM(item) \
89: MACRO_BEGIN \
90: print_define("", #item); \
91: printf("%#010x\n", item); \
92: MACRO_END
93:
94: #define PRINT_DEFINE(macro) \
95: MACRO_BEGIN \
96: print_define("", #macro); \
97: printf("%s\n", STRINGIFY(macro)); \
98: MACRO_END
99:
100: #define PRINT_CONSTANT(macro) \
101: MACRO_BEGIN \
102: print_define("", #macro); \
103: printf("%#010x\n", macro); \
104: MACRO_END
105:
106: #define PRINT_REGADDR(macro) \
107: MACRO_BEGIN \
108: print_define("", #macro); \
109: printf("%#010x\n", ¯o); \
110: MACRO_END
111:
112: #define PRINT_REG_PAIR(struct_ptr, name0, name1) \
113: MACRO_BEGIN \
114: print_define(#struct_ptr, #name0 "_" #name1); \
115: printf("%#010x\n", (& (((struct_ptr)0)->name0##_##name1))); \
116: MACRO_END
117:
118: #define PRINT_BIT_POS(reg_type, field) \
119: MACRO_BEGIN \
120: reg_type __reg; \
121: CONTENTS(__reg) = 0; \
122: __reg.field = 1; \
123: print_define(#reg_type, #field "_BIT"); \
124: printf("%d\n", bit_num(#reg_type, #field, CONTENTS(__reg))); \
125: MACRO_END
126:
127: #define PRINT_L2_SIZE(type) \
128: MACRO_BEGIN \
129: print_define("L2_SIZE", #type); \
130: printf("%d\n", log2(sizeof(type), #type)); \
131: MACRO_END
132:
133: #define PRINT_L2_CONSTANT(macro) \
134: MACRO_BEGIN \
135: print_define("L2", #macro); \
136: printf("%d\n", log2(macro, #macro)); \
137: MACRO_END
138:
139: #define PRINT_SIZEOF(type) \
140: MACRO_BEGIN \
141: print_define("SIZEOF", #type); \
142: printf("%d\n", sizeof(type), #type); \
143: MACRO_END
144:
145:
146: char *progname;
147:
148: unsigned bit_num(char *reg_type, char *field, unsigned bits)
149: {
150: unsigned bit;
151: unsigned mask;
152:
153: for (bit = 0, mask = 0x1;
154: (mask & bits) == 0 && mask;
155: mask <<= 1, bit += 1)
156: continue;
157: if (mask)
158: return bit;
159: fprintf(stderr, "%s: Bad BIT_POS for %s.%s\n", progname,
160: reg_type, field);
161: exit(1);
162: return 0;
163: }
164:
165: unsigned field_width(char *reg_type, char *field, unsigned bits)
166: {
167: unsigned width;
168:
169: while (bits && (bits & 0x1) == 0)
170: bits >>= 1;
171: for (width = 0; (bits & 0x1) == 1; bits >>= 1, width += 1)
172: continue;
173: if (bits == 0 && width)
174: return width;
175: fprintf(stderr, "%s: Bad BIT_FIELD for %s.%s\n", progname,
176: reg_type, field);
177: exit(1);
178: return 0;
179: }
180:
181: unsigned log2(unsigned val, char *type)
182: {
183: unsigned l2 = 0;
184:
185: if (val == 0) {
186: fprintf(stderr, "log2: sizeof(%s) is zero!\n", type);
187: exit(1);
188: }
189: while ((val & 0x1) == 0) {
190: l2 += 1;
191: val >>= 1;
192: }
193: if (val != 0x1) {
194: fprintf(stderr, "log2: sizeof(%s) is not power of two!\n",
195: type);
196: exit(1);
197: }
198: return l2;
199: }
200:
201: const char *skip_white(const char *cp)
202: {
203: while (*cp && isspace(*cp))
204: cp += 1;
205: return cp;
206: }
207:
208: const char *strip_prefix(const char *cp, const char *prefix)
209: {
210: int len;
211:
212: cp = skip_white(cp);
213: len = strlen(prefix);
214: if (strncmp(cp, prefix, len) && isspace(*(cp+len)))
215: cp += len;
216: return cp;
217: }
218:
219: void print_define(char *type_name, char *field)
220: {
221: const char *cp;
222: int col = 0;
223:
224: printf("#define\t");
225: if (type_name != NULL && *type_name != '\0') {
226: cp = strip_prefix(type_name, "struct");
227: cp = strip_prefix(cp, "enum");
228: cp = skip_white(cp);
229: for (; *cp; cp++) {
230: if (isspace(*cp))
231: break;
232: if (*cp == '*')
233: break;
234: if (strncmp(cp, "_t", 2) == 0 && !isalnum(cp[2]))
235: break;
236: putchar(isalpha(*cp) ? toupper(*cp) : *cp);
237: col += 1;
238:
239: }
240: putchar('_');
241: col += 1;
242: }
243: for (cp = field; *cp; cp++) {
244: if (*cp == '.')
245: putchar('_');
246: else
247: putchar(isalpha(*cp) ? toupper(*cp) : *cp);
248: col += 1;
249: }
250: do {
251: putchar(' ');
252: col += 1;
253: } while (col < NAME_LEN);
254: }
255:
256: typedef enum {
257: MAJOR, MINOR
258: } cmt_level_t;
259:
260: void comment(cmt_level_t level, const char *cmt)
261: {
262: switch (level) {
263: case MAJOR:
264: printf("\n\n");
265: printf("//\n");
266: printf("// %s\n", cmt);
267: printf("//\n");
268: break;
269: case MINOR:
270: printf("\n");
271: printf("// %s\n", cmt);
272: printf("\n");
273: break;
274: default:
275: fprintf(stderr, "%s: Bad comment level\n", progname);
276: exit(1);
277: }
278: }
279:
280: int main(int argc, char **argv)
281: {
282: progname = argv[0];
283:
284: printf("// diskstruct.h -- generated by gendisk.c\n");
285: printf("// DON'T EDIT THIS -- change gendisk.c\n");
286:
287: comment(MAJOR, "Structure Offsets");
288:
289: comment(MINOR, "disk_label_t offsets");
290: PRINT_OFFSET(disk_label_t *, dl_version);
291: PRINT_OFFSET(disk_label_t *, dl_label_blkno);
292: PRINT_OFFSET(disk_label_t *, dl_size);
293: PRINT_OFFSET(disk_label_t *, dl_label);
294: PRINT_OFFSET(disk_label_t *, dl_flags);
295: PRINT_OFFSET(disk_label_t *, dl_tag);
296: PRINT_OFFSET(disk_label_t *, dl_dt);
297: PRINT_OFFSET(disk_label_t *, dl_un);
298: PRINT_OFFSET(disk_label_t *, dl_checksum);
299: PRINT_SIZEOF(disk_label_t);
300: PRINT_SIZEOF(dl_un_t);
301:
302: comment(MINOR, "disktab_t offsets");
303: PRINT_OFFSET(disktab_t *, d_name);
304: PRINT_OFFSET(disktab_t *, d_type);
305: PRINT_OFFSET(disktab_t *, d_secsize);
306: PRINT_OFFSET(disktab_t *, d_ntracks);
307: PRINT_OFFSET(disktab_t *, d_nsectors);
308: PRINT_OFFSET(disktab_t *, d_ncylinders);
309: PRINT_OFFSET(disktab_t *, d_rpm);
310: PRINT_OFFSET(disktab_t *, d_front);
311: PRINT_OFFSET(disktab_t *, d_back);
312: PRINT_OFFSET(disktab_t *, d_ngroups);
313: PRINT_OFFSET(disktab_t *, d_ag_size);
314: PRINT_OFFSET(disktab_t *, d_ag_alts);
315: PRINT_OFFSET(disktab_t *, d_ag_off);
316: PRINT_OFFSET(disktab_t *, d_boot0_blkno);
317: PRINT_OFFSET(disktab_t *, d_bootfile);
318: PRINT_OFFSET(disktab_t *, d_hostname);
319: PRINT_OFFSET(disktab_t *, d_rootpartition);
320: PRINT_OFFSET(disktab_t *, d_rwpartition);
321: PRINT_OFFSET(disktab_t *, d_partitions);
322: PRINT_SIZEOF(disktab_t);
323:
324: comment(MINOR, "partition_t offsets");
325: PRINT_OFFSET(partition_t *, p_base);
326: PRINT_OFFSET(partition_t *, p_size);
327: PRINT_OFFSET(partition_t *, p_bsize);
328: PRINT_OFFSET(partition_t *, p_fsize);
329: PRINT_OFFSET(partition_t *, p_opt);
330: PRINT_OFFSET(partition_t *, p_cpg);
331: PRINT_OFFSET(partition_t *, p_density);
332: PRINT_OFFSET(partition_t *, p_minfree);
333: PRINT_OFFSET(partition_t *, p_newfs);
334: PRINT_OFFSET(partition_t *, p_mountpt);
335: PRINT_OFFSET(partition_t *, p_automnt);
336: PRINT_OFFSET(partition_t *, p_type);
337: PRINT_SIZEOF(partition_t);
338:
339: return 0;
340: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.