]> git.saurik.com Git - apple/boot.git/blob - gen/libsaio/ufs_byteorder.c
boot-111.tar.gz
[apple/boot.git] / gen / libsaio / ufs_byteorder.c
1 /*
2 * Copyright (c) 1999 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 #import <sys/types.h>
31 #import <sys/param.h>
32 #import <sys/vnode.h>
33 #import <ufs/fsdir.h>
34 #include <libkern/OSByteOrder.h>
35 #import "ufs_byteorder.h"
36 #import "libsaio.h"
37
38 #define swapBigLongToHost(thing) ((thing) = OSSwapBigToHostInt32(thing))
39 #define swapBigShortToHost(thing) ((thing) = OSSwapBigToHostInt16(thing))
40
41 void
42 swapBigIntsToHost(int *array, int count)
43 {
44 register int i;
45
46 for (i = 0; i < count; i++)
47 swapBigLongToHost(array[i]);
48 }
49
50
51 void
52 swapBigShortToHosts(short *array, int count)
53 {
54 register int i;
55
56 for (i = 0; i < count; i++)
57 swapBigShortToHost(array[i]);
58 }
59
60
61 void
62 byte_swap_superblock(struct fs *sb)
63 {
64 swapBigIntsToHost(((int *) sb) + 2, 50);
65
66 swapBigLongToHost(sb->fs_cgrotor);
67 swapBigLongToHost(sb->fs_cpc);
68
69 swapBigShortToHosts((short *) sb->fs_postbl, MAXCPG * NRPOS);
70
71 swapBigLongToHost(sb->fs_magic);
72 }
73
74
75 static inline void
76 byte_swap_disklabel_common(disk_label_t *dl)
77 {
78 swapBigLongToHost(dl->dl_version); /* ditto */
79 swapBigLongToHost(dl->dl_label_blkno);
80 swapBigLongToHost(dl->dl_size);
81 swapBigLongToHost(dl->dl_flags);
82 swapBigLongToHost(dl->dl_tag);
83 // swapBigShortToHost(dl->dl_checksum);
84 // if (dl->dl_version >= DL_V3)
85 // swapBigShortToHost(dl->dl_un.DL_v3_checksum);
86 // else
87 // swapBigIntsToHost(dl->dl_un.DL_bad, NBAD);
88 }
89
90
91 void
92 byte_swap_disklabel_in(disk_label_t *dl)
93 {
94 byte_swap_disklabel_common(dl);
95 byte_swap_disktab_in(&dl->dl_dt);
96 }
97
98
99 static inline void
100 byte_swap_disktab_common(struct disktab *dt)
101 {
102 register unsigned int i;
103
104 swapBigLongToHost(dt->d_secsize);
105 swapBigLongToHost(dt->d_ntracks);
106 swapBigLongToHost(dt->d_nsectors);
107 swapBigLongToHost(dt->d_ncylinders);
108 // swapBigLongToHost(dt->d_rpm);
109 swapBigShortToHost(dt->d_front);
110 swapBigShortToHost(dt->d_back);
111 // swapBigShortToHost(dt->d_ngroups);
112 // swapBigShortToHost(dt->d_ag_size);
113 // swapBigShortToHost(dt->d_ag_alts);
114 // swapBigShortToHost(dt->d_ag_off);
115 // swapBigIntsToHost(dt->d_boot0_blkno, NBOOTS);
116
117 for (i=0; i < NPART; i++)
118 byte_swap_partition(&dt->d_partitions[i]);
119 }
120
121 /*
122 * This is particularly grody. The beginning of the partition array is two
123 * bytes low on the 68 wrt natural alignment rules. Furthermore, each
124 * element of the partition table is two bytes smaller on 68k due to padding
125 * at the end of the struct.
126 */
127 void
128 byte_swap_disktab_in(struct disktab *dt)
129 {
130 struct partition *pp;
131 int i;
132
133 /*
134 * Shift each struct partition up in memory by 2 + 2 * offset bytes.
135 * Do it backwards so we don't overwrite anything.
136 */
137 for (i=NPART - 1; i >=0; i--) {
138 struct partition temp;
139 pp = &dt->d_partitions[i];
140 /* beware: compiler doesn't do overlapping struct assignment */
141 temp = *(struct partition *)(((char *) pp) - 2 * (i + 1));
142 *pp = temp;
143 }
144
145 byte_swap_disktab_common(dt);
146 }
147
148
149 void
150 byte_swap_partition(struct partition *part)
151 {
152 swapBigLongToHost(part->p_base);
153 swapBigLongToHost(part->p_size);
154 swapBigShortToHost(part->p_bsize);
155 swapBigShortToHost(part->p_fsize);
156 swapBigShortToHost(part->p_cpg);
157 swapBigShortToHost(part->p_density);
158 }
159
160 #if NOTUSED
161 void
162 byte_swap_csum(struct csum *cs)
163 {
164 swapBigIntsToHost((int *) cs, sizeof(struct csum) / sizeof(int));
165 }
166
167
168 void
169 byte_swap_cylgroup(struct cg *cg)
170 {
171 swapBigLongToHost(cg->cg_time);
172 swapBigLongToHost(cg->cg_cgx);
173 swapBigShortToHost(cg->cg_ncyl);
174 swapBigShortToHost(cg->cg_niblk);
175 swapBigLongToHost(cg->cg_ndblk);
176 byte_swap_csum(&cg->cg_cs);
177 swapBigLongToHost(cg->cg_rotor);
178 swapBigLongToHost(cg->cg_frotor);
179 swapBigLongToHost(cg->cg_irotor);
180 swapBigIntsToHost(cg->cg_frsum, MAXFRAG);
181 swapBigIntsToHost(cg->cg_btot, MAXCPG);
182 swapBigShortToHosts((short *) cg->cg_b, MAXCPG * NRPOS);
183 swapBigLongToHost(cg->cg_magic);
184 }
185 #endif
186
187
188 void
189 byte_swap_inode_in(struct icommon *dc, struct icommon *ic)
190 {
191 register int i;
192
193 ic->ic_mode = NXSwapBigShortToHost(dc->ic_mode);
194 ic->ic_nlink = NXSwapBigShortToHost(dc->ic_nlink);
195 // ic->ic_uid = NXSwapBigShortToHost(dc->ic_uid);
196 // ic->ic_gid = NXSwapBigShortToHost(dc->ic_gid);
197
198 ic->ic_size.val[0] = NXSwapBigLongToHost(dc->ic_size.val[1]);
199 ic->ic_size.val[1] = NXSwapBigLongToHost(dc->ic_size.val[0]);
200
201 // ic->ic_atime = NXSwapBigLongToHost(dc->ic_atime);
202 // ic->ic_mtime = NXSwapBigLongToHost(dc->ic_mtime);
203 // ic->ic_ctime = NXSwapBigLongToHost(dc->ic_ctime);
204 // ic->ic_atspare = NXSwapBigLongToHost(dc->ic_atspare);
205 // ic->ic_mtspare = NXSwapBigLongToHost(dc->ic_mtspare);
206 // ic->ic_ctspare = NXSwapBigLongToHost(dc->ic_ctspare);
207
208 ic->ic_flags = NXSwapBigLongToHost(dc->ic_flags);
209
210 if ((ic->ic_flags & IC_FASTLINK) == 0) { /* not a fast symlink */
211
212 for (i=0; i < NDADDR; i++) /* direct blocks */
213 ic->ic_db[i] = NXSwapBigLongToHost(dc->ic_db[i]);
214
215 for (i=0; i < NIADDR; i++) /* indirect blocks */
216 ic->ic_ib[i] = NXSwapBigLongToHost(dc->ic_ib[i]);
217 }
218 else
219 bcopy(dc->ic_symlink, ic->ic_symlink, sizeof(dc->ic_symlink));
220
221 ic->ic_blocks = NXSwapBigLongToHost(dc->ic_blocks);
222 ic->ic_gen = NXSwapBigLongToHost(dc->ic_gen);
223 for (i=0; i < sizeof(ic->ic_spare) / sizeof(int); i++)
224 ic->ic_spare[i] = NXSwapBigLongToHost(dc->ic_spare[i]);
225 }
226
227
228 void
229 byte_swap_dir_block_in(char *addr, int count)
230 {
231 register struct direct *ep = (struct direct *) addr;
232 register int entryoffsetinblk = 0;
233
234 while (entryoffsetinblk < count) {
235 ep = (struct direct *) (entryoffsetinblk + addr);
236 swapBigLongToHost(ep->d_ino);
237 swapBigShortToHost(ep->d_reclen);
238 swapBigShortToHost(ep->d_namlen);
239 entryoffsetinblk += ep->d_reclen;
240 if (ep->d_reclen < 12) /* handle garbage in dirs */
241 break;
242 }
243 }