apm.c revision 268161
134689Sbde/*- 250476Speter * Copyright (c) 2014 Juniper Networks, Inc. 31573Srgrimes * All rights reserved. 4156813Sru * 5156813Sru * Redistribution and use in source and binary forms, with or without 634689Sbde * modification, are permitted provided that the following conditions 734689Sbde * are met: 834689Sbde * 1. Redistributions of source code must retain the above copyright 938752Sbde * notice, this list of conditions and the following disclaimer. 10173017Sru * 2. Redistributions in binary form must reproduce the above copyright 11228989Srwatson * notice, this list of conditions and the following disclaimer in the 12204738Simp * documentation and/or other materials provided with the distribution. 13204738Simp * 1481133Stmm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15255597Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1659897Sjoe * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1779471Smarkm * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18166131Srafan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19122568Sharti * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2059353Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2141257Sjdp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2282355Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2394690Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2441257Sjdp * SUCH DAMAGE. 2556081Sbde */ 2694690Sdes 27181344Sdfr#include <sys/cdefs.h> 2834689Sbde__FBSDID("$FreeBSD: stable/10/usr.bin/mkimg/apm.c 268161 2014-07-02 14:54:41Z marcel $"); 2934689Sbde 30204738Simp#include <sys/types.h> 31204738Simp#include <sys/apm.h> 3234689Sbde#include <sys/endian.h> 33205113Simp#include <sys/errno.h> 34205113Simp#include <stdlib.h> 35258398Speter#include <string.h> 36205113Simp#include <unistd.h> 37205113Simp 38215127Sed#include "image.h" 39205113Simp#include "mkimg.h" 40205113Simp#include "scheme.h" 41219019Sgabor 42205113Simp#ifndef APM_ENT_TYPE_FREEBSD_NANDFS 43255601Sdes#define APM_ENT_TYPE_FREEBSD_NANDFS "FreeBSD-nandfs" 44205113Simp#endif 45205113Simp 46205113Simpstatic struct mkimg_alias apm_aliases[] = { 47205113Simp { ALIAS_FREEBSD, ALIAS_PTR2TYPE(APM_ENT_TYPE_FREEBSD) }, 48205113Simp { ALIAS_FREEBSD_BOOT, ALIAS_PTR2TYPE(APM_ENT_TYPE_APPLE_BOOT) }, 49205113Simp { ALIAS_FREEBSD_NANDFS, ALIAS_PTR2TYPE(APM_ENT_TYPE_FREEBSD_NANDFS) }, 50205113Simp { ALIAS_FREEBSD_SWAP, ALIAS_PTR2TYPE(APM_ENT_TYPE_FREEBSD_SWAP) }, 51205113Simp { ALIAS_FREEBSD_UFS, ALIAS_PTR2TYPE(APM_ENT_TYPE_FREEBSD_UFS) }, 52205113Simp { ALIAS_FREEBSD_VINUM, ALIAS_PTR2TYPE(APM_ENT_TYPE_FREEBSD_VINUM) }, 53227987Sdim { ALIAS_FREEBSD_ZFS, ALIAS_PTR2TYPE(APM_ENT_TYPE_FREEBSD_ZFS) }, 54227987Sdim { ALIAS_NONE, 0 } 55227987Sdim}; 56205113Simp 57233337Sstasstatic u_int 58233337Sstasapm_metadata(u_int where) 59233337Sstas{ 60233337Sstas u_int secs; 61205113Simp 62205113Simp secs = (where == SCHEME_META_IMG_START) ? nparts + 2 : 0; 63205113Simp return (secs); 64205113Simp} 65205113Simp 66215127Sedstatic int 67205113Simpapm_write(lba_t imgsz, void *bootcode __unused) 68205113Simp{ 69205113Simp u_char *buf; 70205113Simp struct apm_ddr *ddr; 71205113Simp struct apm_ent *ent; 72205113Simp struct part *part; 73205113Simp int error; 74205113Simp 75205113Simp buf = calloc(nparts + 2, secsz); 76205113Simp if (buf == NULL) 77205113Simp return (ENOMEM); 78255180Semaste ddr = (void *)buf; 79205113Simp be16enc(&ddr->ddr_sig, APM_DDR_SIG); 80205113Simp be16enc(&ddr->ddr_blksize, secsz); 81205113Simp be32enc(&ddr->ddr_blkcount, imgsz); 82205113Simp 83205113Simp /* partition entry for the partition table itself. */ 84205113Simp ent = (void *)(buf + secsz); 85205113Simp be16enc(&ent->ent_sig, APM_ENT_SIG); 86205113Simp be32enc(&ent->ent_pmblkcnt, nparts + 1); 87205113Simp be32enc(&ent->ent_start, 1); 88205113Simp be32enc(&ent->ent_size, nparts + 1); 89207842Smm strncpy(ent->ent_type, APM_ENT_TYPE_SELF, sizeof(ent->ent_type)); 90205113Simp strncpy(ent->ent_name, "Apple", sizeof(ent->ent_name)); 91241774Suqs 92205113Simp STAILQ_FOREACH(part, &partlist, link) { 93205113Simp ent = (void *)(buf + (part->index + 2) * secsz); 94205113Simp be16enc(&ent->ent_sig, APM_ENT_SIG); 95235537Sgber be32enc(&ent->ent_pmblkcnt, nparts + 1); 96244557Sbrooks be32enc(&ent->ent_start, part->block); 97205113Simp be32enc(&ent->ent_size, part->size); 98205113Simp strncpy(ent->ent_type, ALIAS_TYPE2PTR(part->type), 99205113Simp sizeof(ent->ent_type)); 100205113Simp if (part->label != NULL) 101205113Simp strncpy(ent->ent_name, part->label, 102210682Srpaulo sizeof(ent->ent_name)); 103221807Sstas } 104205113Simp 105210690Srpaulo error = image_write(0, buf, nparts + 2); 106205113Simp free(buf); 107205113Simp return (error); 108252356Sdavide} 109205113Simp 110205113Simpstatic struct mkimg_scheme apm_scheme = { 111205113Simp .name = "apm", 112234772Sjlh .description = "Apple Partition Map", 113228904Sed .aliases = apm_aliases, 114205113Simp .metadata = apm_metadata, 115205113Simp .write = apm_write, 116205113Simp .nparts = 4096, 117205113Simp .labellen = APM_ENT_NAMELEN - 1, 118205113Simp .maxsecsz = 4096 119205113Simp}; 120255597Sdes 121205113SimpSCHEME_DEFINE(apm_scheme); 122205113Simp