]>
git.saurik.com Git - apple/bootx.git/blob - bootx.tproj/fs.subproj/ufs_byteorder.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
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
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.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright 1993 NeXT, Inc.
27 * All rights reserved.
30 * ufs_byteorder.c - Functions for endian swapping UFS disk structures.
32 * Copyright (c) 1998-2000 Apple Computer, Inc.
37 #include <architecture/byte_order.h>
39 #include "ufs_byteorder.h"
41 #define swapBigLongToHost(thing) ((thing) = NXSwapBigLongToHost(thing))
42 #define swapBigShortToHost(thing) ((thing) = NXSwapBigShortToHost(thing))
45 swapBigIntsToHost(int *array
, int count
)
49 for (i
= 0; i
< count
; i
++)
50 swapBigLongToHost(array
[i
]);
55 swapBigShortToHosts(short *array
, int count
)
59 for (i
= 0; i
< count
; i
++)
60 swapBigShortToHost(array
[i
]);
65 byte_swap_superblock(struct fs
*sb
)
68 swapBigIntsToHost(((int *) sb
) + 2, 50);
70 swapBigLongToHost(sb
->fs_cgrotor
);
71 swapBigLongToHost(sb
->fs_cpc
);
73 swapBigShortToHosts((short *) sb
->fs_postbl
, MAXCPG
* NRPOS
);
75 swapBigLongToHost(sb
->fs_magic
);
81 byte_swap_disklabel_common(disk_label_t
*dl
)
84 swapBigLongToHost(dl
->dl_version
); /* ditto */
85 swapBigLongToHost(dl
->dl_label_blkno
);
86 swapBigLongToHost(dl
->dl_size
);
87 swapBigLongToHost(dl
->dl_flags
);
88 swapBigLongToHost(dl
->dl_tag
);
89 // swapBigShortToHost(dl->dl_checksum);
90 // if (dl->dl_version >= DL_V3)
91 // swapBigShortToHost(dl->dl_un.DL_v3_checksum);
93 // swapBigIntsToHost(dl->dl_un.DL_bad, NBAD);
99 byte_swap_disklabel_in(disk_label_t
*dl
)
102 byte_swap_disklabel_common(dl
);
103 byte_swap_disktab_in(&dl
->dl_dt
);
109 byte_swap_disktab_common(struct disktab
*dt
)
112 register unsigned int i
;
114 swapBigLongToHost(dt
->d_secsize
);
115 swapBigLongToHost(dt
->d_ntracks
);
116 swapBigLongToHost(dt
->d_nsectors
);
117 swapBigLongToHost(dt
->d_ncylinders
);
118 // swapBigLongToHost(dt->d_rpm);
119 swapBigShortToHost(dt
->d_front
);
120 swapBigShortToHost(dt
->d_back
);
121 // swapBigShortToHost(dt->d_ngroups);
122 // swapBigShortToHost(dt->d_ag_size);
123 // swapBigShortToHost(dt->d_ag_alts);
124 // swapBigShortToHost(dt->d_ag_off);
125 // swapBigIntsToHost(dt->d_boot0_blkno, NBOOTS);
127 for (i
=0; i
< NPART
; i
++)
128 byte_swap_partition(&dt
->d_partitions
[i
]);
133 * This is particularly grody. The beginning of the partition array is two
134 * bytes low on the 68 wrt natural alignment rules. Furthermore, each
135 * element of the partition table is two bytes smaller on 68k due to padding
136 * at the end of the struct.
139 byte_swap_disktab_in(struct disktab
*dt
)
142 struct partition
*pp
;
146 * Shift each struct partition up in memory by 2 + 2 * offset bytes.
147 * Do it backwards so we don't overwrite anything.
149 for (i
=NPART
- 1; i
>=0; i
--) {
150 struct partition temp
;
151 pp
= &dt
->d_partitions
[i
];
152 /* beware: compiler doesn't do overlapping struct assignment */
153 temp
= *(struct partition
*)(((char *) pp
) - 2 * (i
+ 1));
157 byte_swap_disktab_common(dt
);
163 byte_swap_partition(struct partition
*part
)
166 swapBigLongToHost(part
->p_base
);
167 swapBigLongToHost(part
->p_size
);
168 swapBigShortToHost(part
->p_bsize
);
169 swapBigShortToHost(part
->p_fsize
);
170 swapBigShortToHost(part
->p_cpg
);
171 swapBigShortToHost(part
->p_density
);
177 byte_swap_inode_in(struct dinode
*dc
, struct dinode
*ic
)
182 ic
->ic_mode
= NXSwapBigShortToHost(dc
->ic_mode
);
183 ic
->ic_nlink
= NXSwapBigShortToHost(dc
->ic_nlink
);
184 // ic->ic_uid = NXSwapBigShortToHost(dc->ic_uid);
185 // ic->ic_gid = NXSwapBigShortToHost(dc->ic_gid);
187 ic
->ic_size
.val
[0] = NXSwapBigLongToHost(dc
->ic_size
.val
[1]);
188 ic
->ic_size
.val
[1] = NXSwapBigLongToHost(dc
->ic_size
.val
[0]);
190 // ic->ic_atime = NXSwapBigLongToHost(dc->ic_atime);
191 // ic->ic_mtime = NXSwapBigLongToHost(dc->ic_mtime);
192 // ic->ic_ctime = NXSwapBigLongToHost(dc->ic_ctime);
193 // ic->ic_atspare = NXSwapBigLongToHost(dc->ic_atspare);
194 // ic->ic_mtspare = NXSwapBigLongToHost(dc->ic_mtspare);
195 // ic->ic_ctspare = NXSwapBigLongToHost(dc->ic_ctspare);
197 ic
->ic_flags
= NXSwapBigLongToHost(dc
->ic_flags
);
199 if ((ic
->ic_flags
& IC_FASTLINK
) == 0) { /* not a fast symlink */
201 for (i
=0; i
< NDADDR
; i
++) /* direct blocks */
202 ic
->ic_db
[i
] = NXSwapBigLongToHost(dc
->ic_db
[i
]);
204 for (i
=0; i
< NIADDR
; i
++) /* indirect blocks */
205 ic
->ic_ib
[i
] = NXSwapBigLongToHost(dc
->ic_ib
[i
]);
208 bcopy(dc
->ic_symlink
, ic
->ic_symlink
, sizeof(dc
->ic_symlink
));
210 ic
->ic_blocks
= NXSwapBigLongToHost(dc
->ic_blocks
);
211 ic
->ic_gen
= NXSwapBigLongToHost(dc
->ic_gen
);
212 for (i
=0; i
< sizeof(ic
->ic_spare
) / sizeof(int); i
++)
213 ic
->ic_spare
[i
] = NXSwapBigLongToHost(dc
->ic_spare
[i
]);
221 byte_swap_dir_block_in(char *addr
, int count
)
224 register struct direct
*ep
= (struct direct
*) addr
;
225 register int entryoffsetinblk
= 0;
227 while (entryoffsetinblk
< count
) {
228 ep
= (struct direct
*) (entryoffsetinblk
+ addr
);
229 swapBigLongToHost(ep
->d_ino
);
230 swapBigShortToHost(ep
->d_reclen
);
231 swapBigShortToHost(ep
->d_namlen
);
232 entryoffsetinblk
+= ep
->d_reclen
;
233 if (ep
->d_reclen
< 12) /* handle garbage in dirs */