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