1/* $NetBSD: v7fs_endian.c,v 1.1 2011/06/27 11:52:24 uch Exp $ */ 2 3/*- 4 * Copyright (c) 2011 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by UCHIYAMA Yasushi. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32#if HAVE_NBTOOL_CONFIG_H 33#include "nbtool_config.h" 34#endif 35 36#include <sys/cdefs.h> 37__KERNEL_RCSID(0, "$NetBSD: v7fs_endian.c,v 1.1 2011/06/27 11:52:24 uch Exp $"); 38#if defined _KERNEL_OPT 39#include "opt_v7fs.h" 40#endif 41 42#include "v7fs.h" 43#include "v7fs_endian.h" 44#include "v7fs_impl.h" 45 46#ifndef BYTE_ORDER 47#error 48#endif 49 50/* PDP to Little */ 51#define bswap32pdp_le(x) \ 52 ((uint32_t) \ 53 ((((x) & 0xffff0000) >> 16) | \ 54 (((x) & 0x0000ffff) << 16))) 55/* PDP to Big */ 56#define bswap32pdp_be(x) \ 57 ((uint32_t) \ 58 ((((x) & 0xff00ff00) >> 8) | \ 59 (((x) & 0x00ff00ff) << 8))) 60#ifdef V7FS_EI 61static uint32_t val32_normal_order(uint32_t); 62static uint32_t val32_reverse_order(uint32_t); 63#if BYTE_ORDER == LITTLE_ENDIAN 64static uint32_t val32_pdp_to_little(uint32_t); 65#else 66static uint32_t val32_pdp_to_big(uint32_t); 67#endif 68static uint16_t val16_normal_order(uint16_t); 69static uint16_t val16_reverse_order(uint16_t); 70static v7fs_daddr_t val24_reverse_order_read(uint8_t *); 71static void val24_reverse_order_write(v7fs_daddr_t, uint8_t *); 72static v7fs_daddr_t val24_pdp_read(uint8_t *); 73static void val24_pdp_write(v7fs_daddr_t, uint8_t *); 74 75static uint32_t 76val32_normal_order(uint32_t v) 77{ 78 79 return v; 80} 81 82static uint32_t 83val32_reverse_order(uint32_t v) 84{ 85 86 return bswap32(v); 87} 88#if BYTE_ORDER == LITTLE_ENDIAN 89static uint32_t 90val32_pdp_to_little(uint32_t v) 91{ 92 93 return bswap32pdp_le(v); 94} 95#else 96static uint32_t 97val32_pdp_to_big(uint32_t v) 98{ 99 100 return bswap32pdp_be(v); 101} 102#endif 103static uint16_t 104val16_normal_order(uint16_t v) 105{ 106 107 return v; 108} 109 110static uint16_t 111val16_reverse_order(uint16_t v) 112{ 113 114 return bswap16(v); 115} 116 117static v7fs_daddr_t 118val24_reverse_order_read(uint8_t *a) 119{ 120#if BYTE_ORDER == LITTLE_ENDIAN 121 return (a[0] << 16) | (a[1] << 8) | a[2]; 122#else 123 return (a[2] << 16) | (a[1] << 8) | a[0]; 124#endif 125} 126 127static void 128val24_reverse_order_write(v7fs_daddr_t addr, uint8_t *a) 129{ 130#if BYTE_ORDER == LITTLE_ENDIAN 131 a[0] = (addr >> 16) & 0xff; 132 a[1] = (addr >> 8) & 0xff; 133 a[2] = addr & 0xff; 134#else 135 a[0] = addr & 0xff; 136 a[1] = (addr >> 8) & 0xff; 137 a[2] = (addr >> 16) & 0xff; 138#endif 139} 140 141static v7fs_daddr_t 142val24_pdp_read(uint8_t *a) 143{ 144 145 return (a[0] << 16) | a[1] | (a[2] << 8); 146} 147 148static void 149val24_pdp_write(v7fs_daddr_t addr, uint8_t *a) 150{ 151 152 a[0] = (addr >> 16) & 0xff; 153 a[1] = addr & 0xff; 154 a[2] = (addr >> 8) & 0xff; 155} 156 157void 158v7fs_endian_init(struct v7fs_self *fs) 159{ 160 struct endian_conversion_ops *ops = &fs->val; 161 162 switch (fs->endian) 163 { 164#if BYTE_ORDER == LITTLE_ENDIAN 165 case LITTLE_ENDIAN: 166 ops->conv32 = val32_normal_order; 167 ops->conv16 = val16_normal_order; 168 ops->conv24read = val24_normal_order_read; 169 ops->conv24write = val24_normal_order_write; 170 break; 171 case BIG_ENDIAN: 172 ops->conv32 = val32_reverse_order; 173 ops->conv16 = val16_reverse_order; 174 ops->conv24read = val24_reverse_order_read; 175 ops->conv24write = val24_reverse_order_write; 176 break; 177 case PDP_ENDIAN: 178 ops->conv32 = val32_pdp_to_little; 179 ops->conv16 = val16_normal_order; 180 ops->conv24read = val24_pdp_read; 181 ops->conv24write = val24_pdp_write; 182 break; 183#else /* BIG_ENDIAN */ 184 case LITTLE_ENDIAN: 185 ops->conv32 = val32_reverse_order; 186 ops->conv16 = val16_reverse_order; 187 ops->conv24read = val24_reverse_order_read; 188 ops->conv24write = val24_reverse_order_write; 189 break; 190 case BIG_ENDIAN: 191 ops->conv32 = val32_normal_order; 192 ops->conv16 = val16_normal_order; 193 ops->conv24read = val24_normal_order_read; 194 ops->conv24write = val24_normal_order_write; 195 break; 196 case PDP_ENDIAN: 197 ops->conv32 = val32_pdp_to_big; 198 ops->conv16 = val16_reverse_order; 199 ops->conv24read = val24_pdp_read; 200 ops->conv24write = val24_pdp_write; 201 break; 202#endif 203 } 204} 205#endif /* V7FS_EI */ 206v7fs_daddr_t 207val24_normal_order_read(uint8_t *a) 208{ 209 /*(v7fs_daddr_t)cast is required for int 16bit system. */ 210#if BYTE_ORDER == LITTLE_ENDIAN 211 return ((v7fs_daddr_t)a[2] << 16) | ((v7fs_daddr_t)a[1] << 8) | 212 (v7fs_daddr_t)a[0]; 213#else 214 return ((v7fs_daddr_t)a[0] << 16) | ((v7fs_daddr_t)a[1] << 8) | 215 (v7fs_daddr_t)a[2]; 216#endif 217} 218 219void 220val24_normal_order_write(v7fs_daddr_t addr, uint8_t *a) 221{ 222#if BYTE_ORDER == LITTLE_ENDIAN 223 a[0] = addr & 0xff; 224 a[1] = (addr >> 8) & 0xff; 225 a[2] = (addr >> 16) & 0xff; 226#else 227 a[0] = (addr >> 16) & 0xff; 228 a[1] = (addr >> 8) & 0xff; 229 a[2] = addr & 0xff; 230#endif 231} 232