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