]> git.saurik.com Git - apple/bootx.git/blob - bootx.tproj/fs.subproj/ufs_byteorder.c
BootX-46.tar.gz
[apple/bootx.git] / bootx.tproj / fs.subproj / ufs_byteorder.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * Copyright 1993 NeXT, Inc.
27 * All rights reserved.
28 */
29 /*
30 * ufs_byteorder.c - Functions for endian swapping UFS disk structures.
31 *
32 * Copyright (c) 1998-2000 Apple Computer, Inc.
33 *
34 * DRI: Josh de Cesare
35 */
36
37 #include <architecture/byte_order.h>
38
39 #include "ufs_byteorder.h"
40
41 #define swapBigLongToHost(thing) ((thing) = NXSwapBigLongToHost(thing))
42 #define swapBigShortToHost(thing) ((thing) = NXSwapBigShortToHost(thing))
43
44 void
45 swapBigIntsToHost(int *array, int count)
46 {
47 register int i;
48
49 for (i = 0; i < count; i++)
50 swapBigLongToHost(array[i]);
51 }
52
53
54 void
55 swapBigShortToHosts(short *array, int count)
56 {
57 register int i;
58
59 for (i = 0; i < count; i++)
60 swapBigShortToHost(array[i]);
61 }
62
63
64 void
65 byte_swap_superblock(struct fs *sb)
66 {
67 #ifdef notyet
68 swapBigIntsToHost(((int *) sb) + 2, 50);
69
70 swapBigLongToHost(sb->fs_cgrotor);
71 swapBigLongToHost(sb->fs_cpc);
72
73 swapBigShortToHosts((short *) sb->fs_postbl, MAXCPG * NRPOS);
74
75 swapBigLongToHost(sb->fs_magic);
76 #endif
77 }
78
79
80 static inline void
81 byte_swap_disklabel_common(disk_label_t *dl)
82 {
83
84 swapBigLongToHost(dl->dl_version); /* ditto */
85 swapBigLongToHost(dl->dl_label_blkno);
86 swapBigLongToHost(dl->dl_size);
87 swapBigLongToHost(dl->dl_flags);
88 swapBigLongToHost(dl->dl_tag);
89 // swapBigShortToHost(dl->dl_checksum);
90 // if (dl->dl_version >= DL_V3)
91 // swapBigShortToHost(dl->dl_un.DL_v3_checksum);
92 // else
93 // swapBigIntsToHost(dl->dl_un.DL_bad, NBAD);
94
95 }
96
97
98 void
99 byte_swap_disklabel_in(disk_label_t *dl)
100 {
101
102 byte_swap_disklabel_common(dl);
103 byte_swap_disktab_in(&dl->dl_dt);
104
105 }
106
107
108 static inline void
109 byte_swap_disktab_common(struct disktab *dt)
110 {
111
112 register unsigned int i;
113
114 swapBigLongToHost(dt->d_secsize);
115 swapBigLongToHost(dt->d_ntracks);
116 swapBigLongToHost(dt->d_nsectors);
117 swapBigLongToHost(dt->d_ncylinders);
118 // swapBigLongToHost(dt->d_rpm);
119 swapBigShortToHost(dt->d_front);
120 swapBigShortToHost(dt->d_back);
121 // swapBigShortToHost(dt->d_ngroups);
122 // swapBigShortToHost(dt->d_ag_size);
123 // swapBigShortToHost(dt->d_ag_alts);
124 // swapBigShortToHost(dt->d_ag_off);
125 // swapBigIntsToHost(dt->d_boot0_blkno, NBOOTS);
126
127 for (i=0; i < NPART; i++)
128 byte_swap_partition(&dt->d_partitions[i]);
129
130 }
131
132 /*
133 * This is particularly grody. The beginning of the partition array is two
134 * bytes low on the 68 wrt natural alignment rules. Furthermore, each
135 * element of the partition table is two bytes smaller on 68k due to padding
136 * at the end of the struct.
137 */
138 void
139 byte_swap_disktab_in(struct disktab *dt)
140 {
141
142 struct partition *pp;
143 int i;
144
145 /*
146 * Shift each struct partition up in memory by 2 + 2 * offset bytes.
147 * Do it backwards so we don't overwrite anything.
148 */
149 for (i=NPART - 1; i >=0; i--) {
150 struct partition temp;
151 pp = &dt->d_partitions[i];
152 /* beware: compiler doesn't do overlapping struct assignment */
153 temp = *(struct partition *)(((char *) pp) - 2 * (i + 1));
154 *pp = temp;
155 }
156
157 byte_swap_disktab_common(dt);
158
159 }
160
161
162 void
163 byte_swap_partition(struct partition *part)
164 {
165
166 swapBigLongToHost(part->p_base);
167 swapBigLongToHost(part->p_size);
168 swapBigShortToHost(part->p_bsize);
169 swapBigShortToHost(part->p_fsize);
170 swapBigShortToHost(part->p_cpg);
171 swapBigShortToHost(part->p_density);
172
173 }
174
175
176 void
177 byte_swap_inode_in(struct dinode *dc, struct dinode *ic)
178 {
179 #ifdef notyet
180 register int i;
181
182 ic->ic_mode = NXSwapBigShortToHost(dc->ic_mode);
183 ic->ic_nlink = NXSwapBigShortToHost(dc->ic_nlink);
184 // ic->ic_uid = NXSwapBigShortToHost(dc->ic_uid);
185 // ic->ic_gid = NXSwapBigShortToHost(dc->ic_gid);
186
187 ic->ic_size.val[0] = NXSwapBigLongToHost(dc->ic_size.val[1]);
188 ic->ic_size.val[1] = NXSwapBigLongToHost(dc->ic_size.val[0]);
189
190 // ic->ic_atime = NXSwapBigLongToHost(dc->ic_atime);
191 // ic->ic_mtime = NXSwapBigLongToHost(dc->ic_mtime);
192 // ic->ic_ctime = NXSwapBigLongToHost(dc->ic_ctime);
193 // ic->ic_atspare = NXSwapBigLongToHost(dc->ic_atspare);
194 // ic->ic_mtspare = NXSwapBigLongToHost(dc->ic_mtspare);
195 // ic->ic_ctspare = NXSwapBigLongToHost(dc->ic_ctspare);
196
197 ic->ic_flags = NXSwapBigLongToHost(dc->ic_flags);
198
199 if ((ic->ic_flags & IC_FASTLINK) == 0) { /* not a fast symlink */
200
201 for (i=0; i < NDADDR; i++) /* direct blocks */
202 ic->ic_db[i] = NXSwapBigLongToHost(dc->ic_db[i]);
203
204 for (i=0; i < NIADDR; i++) /* indirect blocks */
205 ic->ic_ib[i] = NXSwapBigLongToHost(dc->ic_ib[i]);
206 }
207 else
208 bcopy(dc->ic_symlink, ic->ic_symlink, sizeof(dc->ic_symlink));
209
210 ic->ic_blocks = NXSwapBigLongToHost(dc->ic_blocks);
211 ic->ic_gen = NXSwapBigLongToHost(dc->ic_gen);
212 for (i=0; i < sizeof(ic->ic_spare) / sizeof(int); i++)
213 ic->ic_spare[i] = NXSwapBigLongToHost(dc->ic_spare[i]);
214 #else
215 *ic = *dc;
216 #endif
217 }
218
219
220 void
221 byte_swap_dir_block_in(char *addr, int count)
222 {
223 #ifdef notyet
224 register struct direct *ep = (struct direct *) addr;
225 register int entryoffsetinblk = 0;
226
227 while (entryoffsetinblk < count) {
228 ep = (struct direct *) (entryoffsetinblk + addr);
229 swapBigLongToHost(ep->d_ino);
230 swapBigShortToHost(ep->d_reclen);
231 swapBigShortToHost(ep->d_namlen);
232 entryoffsetinblk += ep->d_reclen;
233 if (ep->d_reclen < 12) /* handle garbage in dirs */
234 break;
235 }
236 #endif
237 }