]> git.saurik.com Git - apple/boot.git/blame - i386/libsaio/ufs_byteorder.c
boot-93.tar.gz
[apple/boot.git] / i386 / libsaio / ufs_byteorder.c
CommitLineData
14c7c974
A
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
75b89a82
A
29#include <sys/types.h>
30#include <sys/param.h>
31#include <sys/vnode.h>
14c7c974
A
32#include <ufs/ufs/dir.h>
33#include <architecture/byte_order.h>
34#include "ufs_byteorder.h"
35#include "libsaio.h"
36
37#define swapBigLongToHost(thing) ((thing) = NXSwapBigLongToHost(thing))
38#define swapBigShortToHost(thing) ((thing) = NXSwapBigShortToHost(thing))
39#define byte_swap_longlong(thing) ((thing) = NXSwapBigLongLongToHost(thing))
40#define byte_swap_int(thing) ((thing) = NXSwapBigLongToHost(thing))
41#define byte_swap_short(thing) ((thing) = NXSwapBigShortToHost(thing))
42
43void
44byte_swap_longlongs(unsigned long long *array, int count)
45{
46 register unsigned long long i;
47
48 for (i = 0; i < count; i++)
49 byte_swap_longlong(array[i]);
50}
51
52void
53byte_swap_ints(int *array, int count)
54{
55 register int i;
56
57 for (i = 0; i < count; i++)
58 byte_swap_int(array[i]);
59}
60
61void
62byte_swap_shorts(short *array, int count)
63{
64 register int i;
65
66 for (i = 0; i < count; i++)
67 byte_swap_short(array[i]);
68}
69
70void
71swapBigIntsToHost(int *array, int count)
72{
73 register int i;
74
75 for (i = 0; i < count; i++)
76 swapBigLongToHost(array[i]);
77}
78
79
80void
81swapBigShortToHosts(short *array, int count)
82{
83 register int i;
84
85 for (i = 0; i < count; i++)
86 swapBigShortToHost(array[i]);
87}
88
89void
90byte_swap_superblock(struct fs *sb)
91{
92 u_int16_t * usptr;
93 unsigned long size;
94
95 byte_swap_ints(((int32_t *)&sb->fs_firstfield), 52);
96 byte_swap_int(sb->fs_cgrotor);
97 byte_swap_int(sb->fs_cpc);
98 byte_swap_shorts((int16_t *)sb->fs_opostbl, 16 * 8);
99 byte_swap_ints((int32_t *)sb->fs_sparecon, 50);
100 byte_swap_ints((int32_t *)&sb->fs_contigsumsize, 3);
101 byte_swap_longlongs((u_int64_t *)&sb->fs_maxfilesize,3);
102 byte_swap_ints((int32_t *)&sb->fs_state, 6);
103
104 /* Got these magic numbers from mkfs.c in newfs */
105 if (sb->fs_nrpos != 8 || sb->fs_cpc > 16) {
106 usptr = (u_int16_t *)((u_int8_t *)(sb) + (sb)->fs_postbloff);
107 size = sb->fs_cpc * sb->fs_nrpos;
108 byte_swap_shorts(usptr,size); /* fs_postbloff */
109 }
110}
111
112static inline void
113byte_swap_disklabel_common(disk_label_t *dl)
114{
115 swapBigLongToHost(dl->dl_version); /* ditto */
116 swapBigLongToHost(dl->dl_label_blkno);
117 swapBigLongToHost(dl->dl_size);
118 swapBigLongToHost(dl->dl_flags);
119 swapBigLongToHost(dl->dl_tag);
120// swapBigShortToHost(dl->dl_checksum);
121// if (dl->dl_version >= DL_V3)
122// swapBigShortToHost(dl->dl_un.DL_v3_checksum);
123// else
124// swapBigIntsToHost(dl->dl_un.DL_bad, NBAD);
125}
126
127void
128byte_swap_disklabel_in(disk_label_t *dl)
129{
130 byte_swap_disklabel_common(dl);
131 byte_swap_disktab_in(&dl->dl_dt);
132}
133
134static inline void
135byte_swap_disktab_common(struct disktab *dt)
136{
137 register unsigned int i;
138
139 swapBigLongToHost(dt->d_secsize);
140 swapBigLongToHost(dt->d_ntracks);
141 swapBigLongToHost(dt->d_nsectors);
142 swapBigLongToHost(dt->d_ncylinders);
143// swapBigLongToHost(dt->d_rpm);
144 swapBigShortToHost(dt->d_front);
145 swapBigShortToHost(dt->d_back);
146// swapBigShortToHost(dt->d_ngroups);
147// swapBigShortToHost(dt->d_ag_size);
148// swapBigShortToHost(dt->d_ag_alts);
149// swapBigShortToHost(dt->d_ag_off);
150// swapBigIntsToHost(dt->d_boot0_blkno, NBOOTS);
151
152 for (i=0; i < NPART; i++)
153 byte_swap_partition(&dt->d_partitions[i]);
154}
155
156/*
157 * This is particularly grody. The beginning of the partition array is two
158 * bytes low on the 68 wrt natural alignment rules. Furthermore, each
159 * element of the partition table is two bytes smaller on 68k due to padding
160 * at the end of the struct.
161 */
162void
163byte_swap_disktab_in(struct disktab *dt)
164{
165 struct partition * pp;
166 int i;
167
168 /*
169 * Shift each struct partition up in memory by 2 + 2 * offset bytes.
170 * Do it backwards so we don't overwrite anything.
171 */
172 for (i=NPART - 1; i >=0; i--) {
173 struct partition temp;
174 pp = &dt->d_partitions[i];
175 /* beware: compiler doesn't do overlapping struct assignment */
176 temp = *(struct partition *)(((char *) pp) - 2 * (i + 1));
177 *pp = temp;
178 }
179
180 byte_swap_disktab_common(dt);
181}
182
183void
184byte_swap_partition(struct partition *part)
185{
186 swapBigLongToHost(part->p_base);
187 swapBigLongToHost(part->p_size);
188 swapBigShortToHost(part->p_bsize);
189 swapBigShortToHost(part->p_fsize);
190 swapBigShortToHost(part->p_cpg);
191 swapBigShortToHost(part->p_density);
192}
193
194/* This value should correspond to the value set in the ffs_mounts */
195
196#define RESYMLNKLEN 60
197
198void
199byte_swap_dinode_in(struct dinode *di)
200{
201 int i;
202
203 di->di_mode = NXSwapShort(di->di_mode);
204 di->di_nlink = NXSwapShort(di->di_nlink);
205#ifdef LFS
206 di->di_u.inumber = NXSwapLong(di->di_u.inumber);
207#else
208 di->di_u.oldids[0] = NXSwapShort(di->di_u.oldids[0]);
209 di->di_u.oldids[1] = NXSwapShort(di->di_u.oldids[1]);
210#endif
211 di->di_size = NXSwapLongLong(di->di_size);
212 di->di_atime = NXSwapLong(di->di_atime);
213 di->di_atimensec = NXSwapLong(di->di_atimensec);
214 di->di_mtime = NXSwapLong(di->di_mtime);
215 di->di_mtimensec = NXSwapLong(di->di_mtimensec);
216 di->di_ctime = NXSwapLong(di->di_ctime);
217 di->di_ctimensec = NXSwapLong(di->di_ctimensec);
218 if (((di->di_mode & IFMT) != IFLNK ) || (di->di_size > RESYMLNKLEN)) {
219 for (i=0; i < NDADDR; i++) /* direct blocks */
220 di->di_db[i] = NXSwapLong(di->di_db[i]);
221 for (i=0; i < NIADDR; i++) /* indirect blocks */
222 di->di_ib[i] = NXSwapLong(di->di_ib[i]);
223 }
224 di->di_flags = NXSwapLong(di->di_flags);
225 di->di_blocks = NXSwapLong(di->di_blocks);
226 di->di_gen = NXSwapLong(di->di_gen);
227 di->di_uid = NXSwapLong(di->di_uid);
228 di->di_gid = NXSwapLong(di->di_gid);
229 di->di_spare[0] = NXSwapLong(di->di_spare[0]);
230 di->di_spare[1] = NXSwapLong(di->di_spare[1]);
231}
232
233void
234byte_swap_dir_block_in(char *addr, int count)
235{
236 register struct direct * ep = (struct direct *) addr;
237 register int entryoffsetinblk = 0;
238
239 while (entryoffsetinblk < count) {
240 ep = (struct direct *) (entryoffsetinblk + addr);
241 swapBigLongToHost(ep->d_ino);
242 swapBigShortToHost(ep->d_reclen);
243 entryoffsetinblk += ep->d_reclen;
244 if (ep->d_reclen < 12) /* handle garbage in dirs */
245 break;
246 }
247}