ebr.c revision 263843
180709Sjake/*-
280709Sjake * Copyright (c) 2014 Juniper Networks, Inc.
380709Sjake * All rights reserved.
480709Sjake *
580709Sjake * Redistribution and use in source and binary forms, with or without
680709Sjake * modification, are permitted provided that the following conditions
780709Sjake * are met:
880709Sjake * 1. Redistributions of source code must retain the above copyright
980709Sjake *    notice, this list of conditions and the following disclaimer.
1080709Sjake * 2. Redistributions in binary form must reproduce the above copyright
1180709Sjake *    notice, this list of conditions and the following disclaimer in the
1280709Sjake *    documentation and/or other materials provided with the distribution.
1380709Sjake *
1481334Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1580709Sjake * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1680709Sjake * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1781334Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1880709Sjake * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1980709Sjake * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2080709Sjake * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2180709Sjake * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2280709Sjake * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2380709Sjake * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2480709Sjake * SUCH DAMAGE.
2580709Sjake */
2680709Sjake
2780709Sjake#include <sys/cdefs.h>
2880709Sjake__FBSDID("$FreeBSD: user/marcel/mkimg/ebr.c 263843 2014-03-27 22:39:22Z marcel $");
2980709Sjake
3080709Sjake#include <sys/types.h>
3180709Sjake#include <sys/diskmbr.h>
3284179Sjake#include <sys/endian.h>
3384179Sjake#include <sys/errno.h>
3488630Sjake#include <stdlib.h>
3588630Sjake#include <string.h>
3688630Sjake#include <unistd.h>
3788630Sjake
3888630Sjake#include "mkimg.h"
3988630Sjake#include "scheme.h"
4088630Sjake
4188630Sjakestatic struct mkimg_alias ebr_aliases[] = {
4288630Sjake    {	ALIAS_FAT32, ALIAS_INT2TYPE(DOSPTYP_FAT32) },
4388630Sjake    {	ALIAS_FREEBSD, ALIAS_INT2TYPE(DOSPTYP_386BSD) },
44154419Skris    {	ALIAS_NONE, 0 }
4588630Sjake};
4688630Sjake
4788630Sjakestatic u_int
4888630Sjakeebr_metadata(u_int where)
4988630Sjake{
5088630Sjake	u_int secs;
5188630Sjake
5288630Sjake	secs = (where == SCHEME_META_PART_BEFORE) ? 1 : 0;
5388630Sjake	return (secs);
5488630Sjake}
5588630Sjake
5688630Sjakestatic void
5788630Sjakeebr_chs(u_char *cyl, u_char *hd, u_char *sec, uint32_t lba __unused)
5888630Sjake{
5988630Sjake
6088630Sjake	*cyl = 0xff;		/* XXX */
6188630Sjake	*hd = 0xff;		/* XXX */
6288630Sjake	*sec = 0xff;		/* XXX */
6388630Sjake}
6488630Sjake
6588630Sjakestatic int
6688630Sjakeebr_write(int fd, lba_t imgsz __unused, void *bootcode __unused)
6788630Sjake{
68114257Sjake	u_char *ebr;
69114257Sjake	struct dos_partition *dp;
7080709Sjake	struct part *part, *next;
71114257Sjake	lba_t block;
72114257Sjake	int error;
73114257Sjake
74114257Sjake	ebr = malloc(secsz);
7588630Sjake	if (ebr == NULL)
7688630Sjake		return (ENOMEM);
7788630Sjake	memset(ebr, 0, secsz);
7888630Sjake	le16enc(ebr + DOSMAGICOFFSET, DOSMAGIC);
7988630Sjake
8088630Sjake	error = 0;
8188630Sjake	STAILQ_FOREACH_SAFE(part, &partlist, link, next) {
8288630Sjake		block = part->block - nsecs;
8388630Sjake		dp = (void *)(ebr + DOSPARTOFF);
8488630Sjake		ebr_chs(&dp->dp_scyl, &dp->dp_shd, &dp->dp_ssect, nsecs);
8588630Sjake		dp->dp_typ = ALIAS_TYPE2INT(part->type);
8688781Sjake		ebr_chs(&dp->dp_ecyl, &dp->dp_ehd, &dp->dp_esect,
8788630Sjake		    part->block + part->size - 1);
88105939Sjake		le32enc(&dp->dp_start, nsecs);
89105939Sjake		le32enc(&dp->dp_size, part->size);
9088630Sjake
9188630Sjake		/* Add link entry */
9280709Sjake		if (next != NULL) {
93182773Smarius			dp++;
94190107Smarius			ebr_chs(&dp->dp_scyl, &dp->dp_shd, &dp->dp_ssect,
9580709Sjake			    next->block - nsecs);
9680709Sjake			dp->dp_typ = DOSPTYP_EXT;
9784179Sjake			ebr_chs(&dp->dp_ecyl, &dp->dp_ehd, &dp->dp_esect,
9884179Sjake			    next->block + next->size - 1);
9980709Sjake			le32enc(&dp->dp_start, next->block - nsecs);
100			le32enc(&dp->dp_size, next->size + nsecs);
101		}
102
103		error = mkimg_seek(fd, block);
104		if (error == 0) {
105			if (write(fd, ebr, secsz) != secsz)
106				error = errno;
107		}
108		if (error)
109			break;
110
111		memset(ebr + DOSPARTOFF, 0, 2 * DOSPARTSIZE);
112	}
113
114	free(ebr);
115	return (error);
116}
117
118static struct mkimg_scheme ebr_scheme = {
119	.name = "ebr",
120	.description = "Extended Boot Record",
121	.aliases = ebr_aliases,
122	.metadata = ebr_metadata,
123	.write = ebr_write,
124	.nparts = 4096,
125	.maxsecsz = 4096
126};
127
128SCHEME_DEFINE(ebr_scheme);
129