]>
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 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright 1993 NeXT, Inc.
24 * All rights reserved.
27 * ufs_byteorder.c - Functions for endian swapping UFS disk structures.
29 * Copyright (c) 1998-2000 Apple Computer, Inc.
34 #include <architecture/byte_order.h>
36 #include "ufs_byteorder.h"
38 #define swapBigLongToHost(thing) ((thing) = NXSwapBigLongToHost(thing))
39 #define swapBigShortToHost(thing) ((thing) = NXSwapBigShortToHost(thing))
42 swapBigIntsToHost(int *array
, int count
)
46 for (i
= 0; i
< count
; i
++)
47 swapBigLongToHost(array
[i
]);
52 swapBigShortToHosts(short *array
, int count
)
56 for (i
= 0; i
< count
; i
++)
57 swapBigShortToHost(array
[i
]);
62 byte_swap_superblock(struct fs
*sb
)
65 swapBigIntsToHost(((int *) sb
) + 2, 50);
67 swapBigLongToHost(sb
->fs_cgrotor
);
68 swapBigLongToHost(sb
->fs_cpc
);
70 swapBigShortToHosts((short *) sb
->fs_postbl
, MAXCPG
* NRPOS
);
72 swapBigLongToHost(sb
->fs_magic
);
78 byte_swap_disklabel_common(disk_label_t
*dl
)
81 swapBigLongToHost(dl
->dl_version
); /* ditto */
82 swapBigLongToHost(dl
->dl_label_blkno
);
83 swapBigLongToHost(dl
->dl_size
);
84 swapBigLongToHost(dl
->dl_flags
);
85 swapBigLongToHost(dl
->dl_tag
);
86 // swapBigShortToHost(dl->dl_checksum);
87 // if (dl->dl_version >= DL_V3)
88 // swapBigShortToHost(dl->dl_un.DL_v3_checksum);
90 // swapBigIntsToHost(dl->dl_un.DL_bad, NBAD);
96 byte_swap_disklabel_in(disk_label_t
*dl
)
99 byte_swap_disklabel_common(dl
);
100 byte_swap_disktab_in(&dl
->dl_dt
);
106 byte_swap_disktab_common(struct disktab
*dt
)
109 register unsigned int i
;
111 swapBigLongToHost(dt
->d_secsize
);
112 swapBigLongToHost(dt
->d_ntracks
);
113 swapBigLongToHost(dt
->d_nsectors
);
114 swapBigLongToHost(dt
->d_ncylinders
);
115 // swapBigLongToHost(dt->d_rpm);
116 swapBigShortToHost(dt
->d_front
);
117 swapBigShortToHost(dt
->d_back
);
118 // swapBigShortToHost(dt->d_ngroups);
119 // swapBigShortToHost(dt->d_ag_size);
120 // swapBigShortToHost(dt->d_ag_alts);
121 // swapBigShortToHost(dt->d_ag_off);
122 // swapBigIntsToHost(dt->d_boot0_blkno, NBOOTS);
124 for (i
=0; i
< NPART
; i
++)
125 byte_swap_partition(&dt
->d_partitions
[i
]);
130 * This is particularly grody. The beginning of the partition array is two
131 * bytes low on the 68 wrt natural alignment rules. Furthermore, each
132 * element of the partition table is two bytes smaller on 68k due to padding
133 * at the end of the struct.
136 byte_swap_disktab_in(struct disktab
*dt
)
139 struct partition
*pp
;
143 * Shift each struct partition up in memory by 2 + 2 * offset bytes.
144 * Do it backwards so we don't overwrite anything.
146 for (i
=NPART
- 1; i
>=0; i
--) {
147 struct partition temp
;
148 pp
= &dt
->d_partitions
[i
];
149 /* beware: compiler doesn't do overlapping struct assignment */
150 temp
= *(struct partition
*)(((char *) pp
) - 2 * (i
+ 1));
154 byte_swap_disktab_common(dt
);
160 byte_swap_partition(struct partition
*part
)
163 swapBigLongToHost(part
->p_base
);
164 swapBigLongToHost(part
->p_size
);
165 swapBigShortToHost(part
->p_bsize
);
166 swapBigShortToHost(part
->p_fsize
);
167 swapBigShortToHost(part
->p_cpg
);
168 swapBigShortToHost(part
->p_density
);
174 byte_swap_inode_in(struct dinode
*dc
, struct dinode
*ic
)
179 ic
->ic_mode
= NXSwapBigShortToHost(dc
->ic_mode
);
180 ic
->ic_nlink
= NXSwapBigShortToHost(dc
->ic_nlink
);
181 // ic->ic_uid = NXSwapBigShortToHost(dc->ic_uid);
182 // ic->ic_gid = NXSwapBigShortToHost(dc->ic_gid);
184 ic
->ic_size
.val
[0] = NXSwapBigLongToHost(dc
->ic_size
.val
[1]);
185 ic
->ic_size
.val
[1] = NXSwapBigLongToHost(dc
->ic_size
.val
[0]);
187 // ic->ic_atime = NXSwapBigLongToHost(dc->ic_atime);
188 // ic->ic_mtime = NXSwapBigLongToHost(dc->ic_mtime);
189 // ic->ic_ctime = NXSwapBigLongToHost(dc->ic_ctime);
190 // ic->ic_atspare = NXSwapBigLongToHost(dc->ic_atspare);
191 // ic->ic_mtspare = NXSwapBigLongToHost(dc->ic_mtspare);
192 // ic->ic_ctspare = NXSwapBigLongToHost(dc->ic_ctspare);
194 ic
->ic_flags
= NXSwapBigLongToHost(dc
->ic_flags
);
196 if ((ic
->ic_flags
& IC_FASTLINK
) == 0) { /* not a fast symlink */
198 for (i
=0; i
< NDADDR
; i
++) /* direct blocks */
199 ic
->ic_db
[i
] = NXSwapBigLongToHost(dc
->ic_db
[i
]);
201 for (i
=0; i
< NIADDR
; i
++) /* indirect blocks */
202 ic
->ic_ib
[i
] = NXSwapBigLongToHost(dc
->ic_ib
[i
]);
205 bcopy(dc
->ic_symlink
, ic
->ic_symlink
, sizeof(dc
->ic_symlink
));
207 ic
->ic_blocks
= NXSwapBigLongToHost(dc
->ic_blocks
);
208 ic
->ic_gen
= NXSwapBigLongToHost(dc
->ic_gen
);
209 for (i
=0; i
< sizeof(ic
->ic_spare
) / sizeof(int); i
++)
210 ic
->ic_spare
[i
] = NXSwapBigLongToHost(dc
->ic_spare
[i
]);
218 byte_swap_dir_block_in(char *addr
, int count
)
221 register struct direct
*ep
= (struct direct
*) addr
;
222 register int entryoffsetinblk
= 0;
224 while (entryoffsetinblk
< count
) {
225 ep
= (struct direct
*) (entryoffsetinblk
+ addr
);
226 swapBigLongToHost(ep
->d_ino
);
227 swapBigShortToHost(ep
->d_reclen
);
228 swapBigShortToHost(ep
->d_namlen
);
229 entryoffsetinblk
+= ep
->d_reclen
;
230 if (ep
->d_reclen
< 12) /* handle garbage in dirs */