geom_sunlabel_enc.c revision 113813
1/*- 2 * Copyright (c) 2003 Jake Burkholder 3 * Copyright (c) 2003 Poul-Henning Kamp 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 * $FreeBSD: head/sys/geom/geom_sunlabel_enc.c 113813 2003-04-21 18:41:12Z phk $ 28 * 29 * Functions to encode or decode struct sun_disklabel into a bytestream 30 * of correct endianess and packing. 31 * 32 * NB! This file must be usable both in kernel and userland. 33 * 34 */ 35 36#include <sys/types.h> 37#include <sys/endian.h> 38#include <sys/errno.h> 39#include <sys/sun_disklabel.h> 40 41#define SL_TEXT 0x0 42#define SL_TEXT_SIZEOF 0x80 43#define SL_RPM 0x1a4 44#define SL_PCYLINDERS 0x1a6 45#define SL_SPARESPERCYL 0x1a8 46#define SL_INTERLEAVE 0x1ae 47#define SL_NCYLINDERS 0x1b0 48#define SL_ACYLINDERS 0x1b2 49#define SL_NTRACKS 0x1b4 50#define SL_NSECTORS 0x1b6 51#define SL_PART 0x1bc 52#define SL_MAGIC 0x1fc 53#define SL_CKSUM 0x1fe 54 55#define SL_LENGTH 0x200 56 57#define SL_NPART 0x8 58 59#define SDKP_CYLOFFSET 0 60#define SDKP_NSECTORS 0x4 61#define SDKP_SIZEOF 0x8 62 63/* 64 * Decode the relevant fields of a sun disk label, and return zero if the 65 * magic and checksum works out OK. 66 */ 67int 68sunlabel_dec(void const *pp, struct sun_disklabel *sl) 69{ 70 const uint8_t *p; 71 size_t i; 72 u_int u; 73 74 p = pp; 75 for (i = 0; i < sizeof(sl->sl_text); i++) 76 sl->sl_text[i] = p[SL_TEXT + i]; 77 sl->sl_rpm = be16dec(p + SL_RPM); 78 sl->sl_pcylinders = be16dec(p + SL_PCYLINDERS); 79 sl->sl_sparespercyl = be16dec(p + SL_SPARESPERCYL); 80 sl->sl_interleave = be16dec(p + SL_INTERLEAVE); 81 sl->sl_ncylinders = be16dec(p + SL_NCYLINDERS); 82 sl->sl_acylinders = be16dec(p + SL_ACYLINDERS); 83 sl->sl_ntracks = be16dec(p + SL_NTRACKS); 84 sl->sl_nsectors = be16dec(p + SL_NSECTORS); 85 for (i = 0; i < SL_NPART; i++) { 86 sl->sl_part[i].sdkp_cyloffset = be32dec(p + SL_PART + 87 (i * SDKP_SIZEOF) + SDKP_CYLOFFSET); 88 sl->sl_part[i].sdkp_nsectors = be32dec(p + SL_PART + 89 (i * SDKP_SIZEOF) + SDKP_NSECTORS); 90 } 91 sl->sl_magic = be16dec(p + SL_MAGIC); 92 for (i = u = 0; i < SL_LENGTH; i += 2) 93 u ^= be16dec(p + i); 94 if (u == 0 && sl->sl_magic == SUN_DKMAGIC) 95 return (0); 96 else 97 return (EINVAL); 98} 99 100/* 101 * Encode the relevant fields into a sun disklabel, compute new checksum. 102 */ 103void 104sunlabel_enc(void *pp, struct sun_disklabel *sl) 105{ 106 uint8_t *p; 107 size_t i; 108 u_int u; 109 110 p = pp; 111 for (i = 0; i < SL_TEXT_SIZEOF; i++) 112 p[SL_TEXT + i] = sl->sl_text[i]; 113 be16enc(p + SL_RPM, sl->sl_rpm); 114 be16enc(p + SL_PCYLINDERS, sl->sl_pcylinders); 115 be16enc(p + SL_SPARESPERCYL, sl->sl_sparespercyl); 116 be16enc(p + SL_INTERLEAVE, sl->sl_interleave); 117 be16enc(p + SL_NCYLINDERS, sl->sl_ncylinders); 118 be16enc(p + SL_ACYLINDERS, sl->sl_acylinders); 119 be16enc(p + SL_NTRACKS, sl->sl_ntracks); 120 be16enc(p + SL_NSECTORS, sl->sl_nsectors); 121 for (i = 0; i < SL_NPART; i++) { 122 be32enc(p + SL_PART + (i * SDKP_SIZEOF) + SDKP_CYLOFFSET, 123 sl->sl_part[i].sdkp_cyloffset); 124 be32enc(p + SL_PART + (i * SDKP_SIZEOF) + SDKP_NSECTORS, 125 sl->sl_part[i].sdkp_nsectors); 126 } 127 be16enc(p + SL_MAGIC, sl->sl_magic); 128 for (i = u = 0; i < SL_LENGTH; i += 2) 129 u ^= be16dec(p + i); 130 be16enc(p + SL_CKSUM, u); 131} 132