]>
git.saurik.com Git - apple/boot.git/blob - gen/libsaio/ufs_byteorder.c
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.
32 #include <libkern/OSByteOrder.h>
33 #import "ufs_byteorder.h"
36 #define swapBigLongToHost(thing) ((thing) = OSSwapBigToHostInt32(thing))
37 #define swapBigShortToHost(thing) ((thing) = OSSwapBigToHostInt16(thing))
40 swapBigIntsToHost(int *array
, int count
)
44 for (i
= 0; i
< count
; i
++)
45 swapBigLongToHost(array
[i
]);
50 swapBigShortToHosts(short *array
, int count
)
54 for (i
= 0; i
< count
; i
++)
55 swapBigShortToHost(array
[i
]);
60 byte_swap_superblock(struct fs
*sb
)
62 swapBigIntsToHost(((int *) sb
) + 2, 50);
64 swapBigLongToHost(sb
->fs_cgrotor
);
65 swapBigLongToHost(sb
->fs_cpc
);
67 swapBigShortToHosts((short *) sb
->fs_postbl
, MAXCPG
* NRPOS
);
69 swapBigLongToHost(sb
->fs_magic
);
74 byte_swap_disklabel_common(disk_label_t
*dl
)
76 swapBigLongToHost(dl
->dl_version
); /* ditto */
77 swapBigLongToHost(dl
->dl_label_blkno
);
78 swapBigLongToHost(dl
->dl_size
);
79 swapBigLongToHost(dl
->dl_flags
);
80 swapBigLongToHost(dl
->dl_tag
);
81 // swapBigShortToHost(dl->dl_checksum);
82 // if (dl->dl_version >= DL_V3)
83 // swapBigShortToHost(dl->dl_un.DL_v3_checksum);
85 // swapBigIntsToHost(dl->dl_un.DL_bad, NBAD);
90 byte_swap_disklabel_in(disk_label_t
*dl
)
92 byte_swap_disklabel_common(dl
);
93 byte_swap_disktab_in(&dl
->dl_dt
);
98 byte_swap_disktab_common(struct disktab
*dt
)
100 register unsigned int i
;
102 swapBigLongToHost(dt
->d_secsize
);
103 swapBigLongToHost(dt
->d_ntracks
);
104 swapBigLongToHost(dt
->d_nsectors
);
105 swapBigLongToHost(dt
->d_ncylinders
);
106 // swapBigLongToHost(dt->d_rpm);
107 swapBigShortToHost(dt
->d_front
);
108 swapBigShortToHost(dt
->d_back
);
109 // swapBigShortToHost(dt->d_ngroups);
110 // swapBigShortToHost(dt->d_ag_size);
111 // swapBigShortToHost(dt->d_ag_alts);
112 // swapBigShortToHost(dt->d_ag_off);
113 // swapBigIntsToHost(dt->d_boot0_blkno, NBOOTS);
115 for (i
=0; i
< NPART
; i
++)
116 byte_swap_partition(&dt
->d_partitions
[i
]);
120 * This is particularly grody. The beginning of the partition array is two
121 * bytes low on the 68 wrt natural alignment rules. Furthermore, each
122 * element of the partition table is two bytes smaller on 68k due to padding
123 * at the end of the struct.
126 byte_swap_disktab_in(struct disktab
*dt
)
128 struct partition
*pp
;
132 * Shift each struct partition up in memory by 2 + 2 * offset bytes.
133 * Do it backwards so we don't overwrite anything.
135 for (i
=NPART
- 1; i
>=0; i
--) {
136 struct partition temp
;
137 pp
= &dt
->d_partitions
[i
];
138 /* beware: compiler doesn't do overlapping struct assignment */
139 temp
= *(struct partition
*)(((char *) pp
) - 2 * (i
+ 1));
143 byte_swap_disktab_common(dt
);
148 byte_swap_partition(struct partition
*part
)
150 swapBigLongToHost(part
->p_base
);
151 swapBigLongToHost(part
->p_size
);
152 swapBigShortToHost(part
->p_bsize
);
153 swapBigShortToHost(part
->p_fsize
);
154 swapBigShortToHost(part
->p_cpg
);
155 swapBigShortToHost(part
->p_density
);
160 byte_swap_csum(struct csum
*cs
)
162 swapBigIntsToHost((int *) cs
, sizeof(struct csum
) / sizeof(int));
167 byte_swap_cylgroup(struct cg
*cg
)
169 swapBigLongToHost(cg
->cg_time
);
170 swapBigLongToHost(cg
->cg_cgx
);
171 swapBigShortToHost(cg
->cg_ncyl
);
172 swapBigShortToHost(cg
->cg_niblk
);
173 swapBigLongToHost(cg
->cg_ndblk
);
174 byte_swap_csum(&cg
->cg_cs
);
175 swapBigLongToHost(cg
->cg_rotor
);
176 swapBigLongToHost(cg
->cg_frotor
);
177 swapBigLongToHost(cg
->cg_irotor
);
178 swapBigIntsToHost(cg
->cg_frsum
, MAXFRAG
);
179 swapBigIntsToHost(cg
->cg_btot
, MAXCPG
);
180 swapBigShortToHosts((short *) cg
->cg_b
, MAXCPG
* NRPOS
);
181 swapBigLongToHost(cg
->cg_magic
);
187 byte_swap_inode_in(struct icommon
*dc
, struct icommon
*ic
)
191 ic
->ic_mode
= NXSwapBigShortToHost(dc
->ic_mode
);
192 ic
->ic_nlink
= NXSwapBigShortToHost(dc
->ic_nlink
);
193 // ic->ic_uid = NXSwapBigShortToHost(dc->ic_uid);
194 // ic->ic_gid = NXSwapBigShortToHost(dc->ic_gid);
196 ic
->ic_size
.val
[0] = NXSwapBigLongToHost(dc
->ic_size
.val
[1]);
197 ic
->ic_size
.val
[1] = NXSwapBigLongToHost(dc
->ic_size
.val
[0]);
199 // ic->ic_atime = NXSwapBigLongToHost(dc->ic_atime);
200 // ic->ic_mtime = NXSwapBigLongToHost(dc->ic_mtime);
201 // ic->ic_ctime = NXSwapBigLongToHost(dc->ic_ctime);
202 // ic->ic_atspare = NXSwapBigLongToHost(dc->ic_atspare);
203 // ic->ic_mtspare = NXSwapBigLongToHost(dc->ic_mtspare);
204 // ic->ic_ctspare = NXSwapBigLongToHost(dc->ic_ctspare);
206 ic
->ic_flags
= NXSwapBigLongToHost(dc
->ic_flags
);
208 if ((ic
->ic_flags
& IC_FASTLINK
) == 0) { /* not a fast symlink */
210 for (i
=0; i
< NDADDR
; i
++) /* direct blocks */
211 ic
->ic_db
[i
] = NXSwapBigLongToHost(dc
->ic_db
[i
]);
213 for (i
=0; i
< NIADDR
; i
++) /* indirect blocks */
214 ic
->ic_ib
[i
] = NXSwapBigLongToHost(dc
->ic_ib
[i
]);
217 bcopy(dc
->ic_symlink
, ic
->ic_symlink
, sizeof(dc
->ic_symlink
));
219 ic
->ic_blocks
= NXSwapBigLongToHost(dc
->ic_blocks
);
220 ic
->ic_gen
= NXSwapBigLongToHost(dc
->ic_gen
);
221 for (i
=0; i
< sizeof(ic
->ic_spare
) / sizeof(int); i
++)
222 ic
->ic_spare
[i
] = NXSwapBigLongToHost(dc
->ic_spare
[i
]);
227 byte_swap_dir_block_in(char *addr
, int count
)
229 register struct direct
*ep
= (struct direct
*) addr
;
230 register int entryoffsetinblk
= 0;
232 while (entryoffsetinblk
< count
) {
233 ep
= (struct direct
*) (entryoffsetinblk
+ addr
);
234 swapBigLongToHost(ep
->d_ino
);
235 swapBigShortToHost(ep
->d_reclen
);
236 swapBigShortToHost(ep
->d_namlen
);
237 entryoffsetinblk
+= ep
->d_reclen
;
238 if (ep
->d_reclen
< 12) /* handle garbage in dirs */