]>
git.saurik.com Git - apple/boot.git/blob - i386/libsaio/ufs_byteorder.c
3b06483f6c4a63d9e167d4b9a372ed9e088a618e
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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
22 * @APPLE_LICENSE_HEADER_END@
25 * Copyright 1993 NeXT, Inc.
26 * All rights reserved.
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <sys/vnode.h>
32 #include <ufs/ufs/dir.h>
33 #include <libkern/OSByteOrder.h>
34 #include "ufs_byteorder.h"
37 #define swapBigLongToHost(thing) ((thing) = OSSwapBigToHostInt32(thing))
38 #define swapBigShortToHost(thing) ((thing) = OSSwapBigToHostInt16(thing))
39 #define byte_swap_longlong(thing) ((thing) = OSSwapBigToHostInt64(thing))
40 #define byte_swap_int(thing) ((thing) = OSSwapBigToHostInt32(thing))
41 #define byte_swap_short(thing) ((thing) = OSSwapBigToHostInt16(thing))
44 byte_swap_longlongs(unsigned long long *array
, int count
)
46 register unsigned long long i
;
48 for (i
= 0; i
< (unsigned long long)count
; i
++)
49 byte_swap_longlong(array
[i
]);
53 byte_swap_ints(int *array
, int count
)
57 for (i
= 0; i
< count
; i
++)
58 byte_swap_int(array
[i
]);
62 byte_swap_shorts(short *array
, int count
)
66 for (i
= 0; i
< count
; i
++)
67 byte_swap_short(array
[i
]);
71 swapBigIntsToHost(int *array
, int count
)
75 for (i
= 0; i
< count
; i
++)
76 swapBigLongToHost(array
[i
]);
81 swapBigShortToHosts(short *array
, int count
)
85 for (i
= 0; i
< count
; i
++)
86 swapBigShortToHost(array
[i
]);
90 byte_swap_superblock(struct fs
*sb
)
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);
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 */
113 byte_swap_disklabel_common(disk_label_t
*dl
)
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);
124 // swapBigIntsToHost(dl->dl_un.DL_bad, NBAD);
128 byte_swap_disklabel_in(disk_label_t
*dl
)
130 byte_swap_disklabel_common(dl
);
131 byte_swap_disktab_in(&dl
->dl_dt
);
135 byte_swap_disktab_common(struct disktab
*dt
)
137 register unsigned int i
;
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);
152 for (i
=0; i
< NPART
; i
++)
153 byte_swap_partition(&dt
->d_partitions
[i
]);
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.
163 byte_swap_disktab_in(struct disktab
*dt
)
165 struct partition
* pp
;
169 * Shift each struct partition up in memory by 2 + 2 * offset bytes.
170 * Do it backwards so we don't overwrite anything.
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));
180 byte_swap_disktab_common(dt
);
184 byte_swap_partition(struct partition
*part
)
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
);
194 /* This value should correspond to the value set in the ffs_mounts */
196 #define RESYMLNKLEN 60
199 byte_swap_dinode_in(struct dinode
*di
)
203 di
->di_mode
= NXSwapShort(di
->di_mode
);
204 di
->di_nlink
= NXSwapShort(di
->di_nlink
);
206 di
->di_u
.inumber
= NXSwapLong(di
->di_u
.inumber
);
208 di
->di_u
.oldids
[0] = NXSwapShort(di
->di_u
.oldids
[0]);
209 di
->di_u
.oldids
[1] = NXSwapShort(di
->di_u
.oldids
[1]);
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
]);
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]);
234 byte_swap_dir_block_in(char *addr
, int count
)
236 register struct direct
* ep
= (struct direct
*) addr
;
237 register int entryoffsetinblk
= 0;
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 */