xref: /netbsd-src/external/bsd/pdisk/dist/convert.c (revision 48a628ae0434c4247b560ad8f2eb1dc06d0dd070)
19428323dSchristos //
29428323dSchristos // convert.c - Little-endian conversion
39428323dSchristos //
49428323dSchristos // Written by Eryk Vershen
59428323dSchristos //
69428323dSchristos // See comments in convert.h
79428323dSchristos //
89428323dSchristos 
99428323dSchristos /*
109428323dSchristos  * Copyright 1996,1997,1998 by Apple Computer, Inc.
119428323dSchristos  *              All Rights Reserved
129428323dSchristos  *
139428323dSchristos  * Permission to use, copy, modify, and distribute this software and
149428323dSchristos  * its documentation for any purpose and without fee is hereby granted,
159428323dSchristos  * provided that the above copyright notice appears in all copies and
169428323dSchristos  * that both the copyright notice and this permission notice appear in
179428323dSchristos  * supporting documentation.
189428323dSchristos  *
199428323dSchristos  * APPLE COMPUTER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
209428323dSchristos  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
219428323dSchristos  * FOR A PARTICULAR PURPOSE.
229428323dSchristos  *
239428323dSchristos  * IN NO EVENT SHALL APPLE COMPUTER BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
249428323dSchristos  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
259428323dSchristos  * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
269428323dSchristos  * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
279428323dSchristos  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
289428323dSchristos  */
299428323dSchristos 
309428323dSchristos #ifdef __linux__
319428323dSchristos #include <endian.h>
32*48a628aeSchristos #elif __NetBSD__
33*48a628aeSchristos #include <machine/endian.h>
349428323dSchristos #else
359428323dSchristos #define LITTLE_ENDIAN 1234
369428323dSchristos #define BIG_ENDIAN 4321
379428323dSchristos #define BYTE_ORDER 4321
389428323dSchristos //#define BYTE_ORDER 1234
399428323dSchristos #endif
409428323dSchristos 
419428323dSchristos #include "convert.h"
429428323dSchristos 
439428323dSchristos 
449428323dSchristos //
459428323dSchristos // Defines
469428323dSchristos //
479428323dSchristos 
489428323dSchristos 
499428323dSchristos //
509428323dSchristos // Types
519428323dSchristos //
529428323dSchristos 
539428323dSchristos 
549428323dSchristos //
559428323dSchristos // Global Constants
569428323dSchristos //
579428323dSchristos 
589428323dSchristos 
599428323dSchristos //
609428323dSchristos // Global Variables
619428323dSchristos //
629428323dSchristos 
639428323dSchristos 
649428323dSchristos //
659428323dSchristos // Forward declarations
669428323dSchristos //
67*48a628aeSchristos void reverse2(uint8_t *bytes);
68*48a628aeSchristos void reverse4(uint8_t *bytes);
699428323dSchristos 
709428323dSchristos 
719428323dSchristos //
729428323dSchristos // Routines
739428323dSchristos //
749428323dSchristos int
convert_dpme(DPME * data,int to_cpu_form)759428323dSchristos convert_dpme(DPME *data, int to_cpu_form)
769428323dSchristos {
779428323dSchristos #if BYTE_ORDER == LITTLE_ENDIAN
789428323dSchristos     // Since we will toss the block if the signature doesn't match
799428323dSchristos     // we don't need to check the signature down here.
80*48a628aeSchristos     reverse2((uint8_t *)&data->dpme_signature);
81*48a628aeSchristos     reverse2((uint8_t *)&data->dpme_reserved_1);
82*48a628aeSchristos     reverse4((uint8_t *)&data->dpme_map_entries);
83*48a628aeSchristos     reverse4((uint8_t *)&data->dpme_pblock_start);
84*48a628aeSchristos     reverse4((uint8_t *)&data->dpme_pblocks);
85*48a628aeSchristos     reverse4((uint8_t *)&data->dpme_lblock_start);
86*48a628aeSchristos     reverse4((uint8_t *)&data->dpme_lblocks);
87*48a628aeSchristos     reverse4((uint8_t *)&data->dpme_flags);
88*48a628aeSchristos     reverse4((uint8_t *)&data->dpme_boot_block);
89*48a628aeSchristos     reverse4((uint8_t *)&data->dpme_boot_bytes);
90*48a628aeSchristos     reverse4((uint8_t *)&data->dpme_load_addr);
91*48a628aeSchristos     reverse4((uint8_t *)&data->dpme_load_addr_2);
92*48a628aeSchristos     reverse4((uint8_t *)&data->dpme_goto_addr);
93*48a628aeSchristos     reverse4((uint8_t *)&data->dpme_goto_addr_2);
94*48a628aeSchristos     reverse4((uint8_t *)&data->dpme_checksum);
959428323dSchristos     convert_bzb((BZB *)data->dpme_bzb, to_cpu_form);
969428323dSchristos #endif
979428323dSchristos     return 0;
989428323dSchristos }
999428323dSchristos 
1009428323dSchristos 
1019428323dSchristos #if BYTE_ORDER == LITTLE_ENDIAN
1029428323dSchristos int
convert_bzb(BZB * data,int to_cpu_form)1039428323dSchristos convert_bzb(BZB *data, int to_cpu_form)
1049428323dSchristos {
1059428323dSchristos     // Since the data here varies according to the type of partition we
1069428323dSchristos     // do not want to convert willy-nilly. We use the flag to determine
1079428323dSchristos     // whether to check for the signature before or after we flip the bytes.
1089428323dSchristos     if (to_cpu_form) {
109*48a628aeSchristos 	reverse4((uint8_t *)&data->bzb_magic);
1109428323dSchristos 	if (data->bzb_magic != BZBMAGIC) {
111*48a628aeSchristos 	    reverse4((uint8_t *)&data->bzb_magic);
1129428323dSchristos 	    if (data->bzb_magic != BZBMAGIC) {
1139428323dSchristos 		return 0;
1149428323dSchristos 	    }
1159428323dSchristos 	}
1169428323dSchristos     } else {
1179428323dSchristos 	if (data->bzb_magic != BZBMAGIC) {
1189428323dSchristos 	    return 0;
1199428323dSchristos 	}
120*48a628aeSchristos 	reverse4((uint8_t *)&data->bzb_magic);
1219428323dSchristos     }
122*48a628aeSchristos     reverse2((uint8_t *)&data->bzb_inode);
123*48a628aeSchristos     reverse4((uint8_t *)&data->bzb_flags);
124*48a628aeSchristos     reverse4((uint8_t *)&data->bzb_tmade);
125*48a628aeSchristos     reverse4((uint8_t *)&data->bzb_tmount);
126*48a628aeSchristos     reverse4((uint8_t *)&data->bzb_tumount);
1279428323dSchristos     return 0;
1289428323dSchristos }
1299428323dSchristos #endif
1309428323dSchristos 
1319428323dSchristos 
1329428323dSchristos int
convert_block0(Block0 * data,int to_cpu_form)1339428323dSchristos convert_block0(Block0 *data, int to_cpu_form)
1349428323dSchristos {
1359428323dSchristos #if BYTE_ORDER == LITTLE_ENDIAN
1369428323dSchristos     DDMap *m;
137*48a628aeSchristos     uint16_t count;
1389428323dSchristos     int i;
1399428323dSchristos 
1409428323dSchristos     // Since this data is optional we do not want to convert willy-nilly.
1419428323dSchristos     // We use the flag to determine whether to check for the signature
1429428323dSchristos     // before or after we flip the bytes and to determine which form of
1439428323dSchristos     // the count to use.
1449428323dSchristos     if (to_cpu_form) {
145*48a628aeSchristos 	reverse2((uint8_t *)&data->sbSig);
1469428323dSchristos 	if (data->sbSig != BLOCK0_SIGNATURE) {
147*48a628aeSchristos 	    reverse2((uint8_t *)&data->sbSig);
1489428323dSchristos 	    if (data->sbSig != BLOCK0_SIGNATURE) {
1499428323dSchristos 		return 0;
1509428323dSchristos 	    }
1519428323dSchristos 	}
1529428323dSchristos     } else {
1539428323dSchristos 	if (data->sbSig != BLOCK0_SIGNATURE) {
1549428323dSchristos 	    return 0;
1559428323dSchristos 	}
156*48a628aeSchristos 	reverse2((uint8_t *)&data->sbSig);
1579428323dSchristos     }
158*48a628aeSchristos     reverse2((uint8_t *)&data->sbBlkSize);
159*48a628aeSchristos     reverse4((uint8_t *)&data->sbBlkCount);
160*48a628aeSchristos     reverse2((uint8_t *)&data->sbDevType);
161*48a628aeSchristos     reverse2((uint8_t *)&data->sbDevId);
162*48a628aeSchristos     reverse4((uint8_t *)&data->sbData);
1639428323dSchristos     if (to_cpu_form) {
164*48a628aeSchristos 	reverse2((uint8_t *)&data->sbDrvrCount);
1659428323dSchristos 	count = data->sbDrvrCount;
1669428323dSchristos     } else {
1679428323dSchristos 	count = data->sbDrvrCount;
168*48a628aeSchristos 	reverse2((uint8_t *)&data->sbDrvrCount);
1699428323dSchristos     }
1709428323dSchristos 
1719428323dSchristos     if (count > 0) {
1729428323dSchristos 	m = (DDMap *) data->sbMap;
1739428323dSchristos 	for (i = 0; i < count; i++) {
174*48a628aeSchristos 	    reverse4((uint8_t *)&m[i].ddBlock);
175*48a628aeSchristos 	    reverse2((uint8_t *)&m[i].ddSize);
176*48a628aeSchristos 	    reverse2((uint8_t *)&m[i].ddType);
1779428323dSchristos 	}
1789428323dSchristos     }
1799428323dSchristos #endif
1809428323dSchristos     return 0;
1819428323dSchristos }
1829428323dSchristos 
1839428323dSchristos 
1849428323dSchristos void
reverse2(uint8_t * bytes)185*48a628aeSchristos reverse2(uint8_t *bytes)
1869428323dSchristos {
187*48a628aeSchristos     uint8_t t;
1889428323dSchristos 
1899428323dSchristos     t = *bytes;
1909428323dSchristos     *bytes = bytes[1];
1919428323dSchristos     bytes[1] = t;
1929428323dSchristos }
1939428323dSchristos 
1949428323dSchristos 
1959428323dSchristos void
reverse4(uint8_t * bytes)196*48a628aeSchristos reverse4(uint8_t *bytes)
1979428323dSchristos {
198*48a628aeSchristos     uint8_t t;
1999428323dSchristos 
2009428323dSchristos     t = *bytes;
2019428323dSchristos     *bytes = bytes[3];
2029428323dSchristos     bytes[3] = t;
2039428323dSchristos     t = bytes[1];
2049428323dSchristos     bytes[1] = bytes[2];
2059428323dSchristos     bytes[2] = t;
2069428323dSchristos }
207