1263409Smarcel/*- 2263409Smarcel * Copyright (c) 2014 Juniper Networks, Inc. 3263409Smarcel * All rights reserved. 4263409Smarcel * 5263409Smarcel * Redistribution and use in source and binary forms, with or without 6263409Smarcel * modification, are permitted provided that the following conditions 7263409Smarcel * are met: 8263409Smarcel * 1. Redistributions of source code must retain the above copyright 9263409Smarcel * notice, this list of conditions and the following disclaimer. 10263409Smarcel * 2. Redistributions in binary form must reproduce the above copyright 11263409Smarcel * notice, this list of conditions and the following disclaimer in the 12263409Smarcel * documentation and/or other materials provided with the distribution. 13263409Smarcel * 14263409Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15263409Smarcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16263409Smarcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17263409Smarcel * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18263409Smarcel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19263409Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20263409Smarcel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21263409Smarcel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22263409Smarcel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23263409Smarcel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24263409Smarcel * SUCH DAMAGE. 25263409Smarcel */ 26263409Smarcel 27263409Smarcel#include <sys/cdefs.h> 28263409Smarcel__FBSDID("$FreeBSD$"); 29263409Smarcel 30263409Smarcel#include <sys/types.h> 31263699Smarcel#include <sys/endian.h> 32263442Smarcel#include <sys/errno.h> 33263409Smarcel#include <sys/vtoc.h> 34263699Smarcel#include <stdio.h> 35263409Smarcel#include <stdlib.h> 36263699Smarcel#include <string.h> 37263699Smarcel#include <unistd.h> 38263409Smarcel 39268161Smarcel#include "image.h" 40263409Smarcel#include "mkimg.h" 41263409Smarcel#include "scheme.h" 42263409Smarcel 43268161Smarcel#ifndef VTOC_TAG_FREEBSD_NANDFS 44268161Smarcel#define VTOC_TAG_FREEBSD_NANDFS 0x0905 45268161Smarcel#endif 46268161Smarcel 47263409Smarcelstatic struct mkimg_alias vtoc8_aliases[] = { 48263699Smarcel { ALIAS_FREEBSD_NANDFS, ALIAS_INT2TYPE(VTOC_TAG_FREEBSD_NANDFS) }, 49263699Smarcel { ALIAS_FREEBSD_SWAP, ALIAS_INT2TYPE(VTOC_TAG_FREEBSD_SWAP) }, 50263699Smarcel { ALIAS_FREEBSD_UFS, ALIAS_INT2TYPE(VTOC_TAG_FREEBSD_UFS) }, 51263699Smarcel { ALIAS_FREEBSD_VINUM, ALIAS_INT2TYPE(VTOC_TAG_FREEBSD_VINUM) }, 52263699Smarcel { ALIAS_FREEBSD_ZFS, ALIAS_INT2TYPE(VTOC_TAG_FREEBSD_NANDFS) }, 53263487Smarcel { ALIAS_NONE, 0 } 54263409Smarcel}; 55263409Smarcel 56272030Smarcelstatic lba_t 57272030Smarcelvtoc8_metadata(u_int where, lba_t blk) 58263409Smarcel{ 59263409Smarcel 60272030Smarcel blk += (where == SCHEME_META_IMG_START) ? 1 : 0; 61272030Smarcel return (round_cylinder(blk)); 62263409Smarcel} 63263409Smarcel 64263442Smarcelstatic int 65268161Smarcelvtoc8_write(lba_t imgsz, void *bootcode __unused) 66263442Smarcel{ 67263699Smarcel struct vtoc8 vtoc8; 68263699Smarcel struct part *part; 69263845Smarcel u_char *p; 70263893Smarcel int error, n; 71263845Smarcel uint16_t ofs, sum; 72263699Smarcel 73268161Smarcel imgsz = (lba_t)ncyls * nheads * nsecs; 74263893Smarcel 75263699Smarcel memset(&vtoc8, 0, sizeof(vtoc8)); 76263893Smarcel sprintf(vtoc8.ascii, "FreeBSD%lldM", 77263893Smarcel (long long)(imgsz * secsz / 1048576)); 78263699Smarcel be32enc(&vtoc8.version, VTOC_VERSION); 79263699Smarcel be16enc(&vtoc8.nparts, VTOC8_NPARTS); 80263699Smarcel be32enc(&vtoc8.sanity, VTOC_SANITY); 81263699Smarcel be16enc(&vtoc8.rpm, 3600); 82263845Smarcel be16enc(&vtoc8.physcyls, ncyls); 83263845Smarcel be16enc(&vtoc8.ncyls, ncyls); 84263845Smarcel be16enc(&vtoc8.altcyls, 0); 85263845Smarcel be16enc(&vtoc8.nheads, nheads); 86263845Smarcel be16enc(&vtoc8.nsecs, nsecs); 87263699Smarcel be16enc(&vtoc8.magic, VTOC_MAGIC); 88263699Smarcel 89263893Smarcel be32enc(&vtoc8.map[VTOC_RAW_PART].nblks, imgsz); 90263699Smarcel STAILQ_FOREACH(part, &partlist, link) { 91263893Smarcel n = part->index + ((part->index >= VTOC_RAW_PART) ? 1 : 0); 92263893Smarcel be16enc(&vtoc8.part[n].tag, ALIAS_TYPE2INT(part->type)); 93263893Smarcel be32enc(&vtoc8.map[n].cyl, part->block / (nsecs * nheads)); 94263893Smarcel be32enc(&vtoc8.map[n].nblks, part->size); 95263699Smarcel } 96263845Smarcel 97263845Smarcel /* Calculate checksum. */ 98263845Smarcel sum = 0; 99263845Smarcel p = (void *)&vtoc8; 100263845Smarcel for (ofs = 0; ofs < sizeof(vtoc8) - 2; ofs += 2) 101263845Smarcel sum ^= be16dec(p + ofs); 102263845Smarcel be16enc(&vtoc8.cksum, sum); 103263845Smarcel 104268161Smarcel error = image_write(0, &vtoc8, 1); 105263699Smarcel return (error); 106263442Smarcel} 107263442Smarcel 108263409Smarcelstatic struct mkimg_scheme vtoc8_scheme = { 109263409Smarcel .name = "vtoc8", 110263409Smarcel .description = "SMI VTOC8 disk labels", 111263409Smarcel .aliases = vtoc8_aliases, 112263440Smarcel .metadata = vtoc8_metadata, 113263442Smarcel .write = vtoc8_write, 114263893Smarcel .nparts = VTOC8_NPARTS - 1, 115263700Smarcel .maxsecsz = 512 116263409Smarcel}; 117263409Smarcel 118263409SmarcelSCHEME_DEFINE(vtoc8_scheme); 119