11638Srgrimes/*	$OpenBSD: build.c,v 1.8 2014/07/12 19:01:50 tedu Exp $	*/
21638Srgrimes
31638Srgrimes/*
41638Srgrimes * Copyright (c) 2004 Theo de Raadt <deraadt@openbsd.org>
51638Srgrimes *
61638Srgrimes * Permission to use, copy, modify, and distribute this software for any
71638Srgrimes * purpose with or without fee is hereby granted, provided that the above
81638Srgrimes * copyright notice and this permission notice appear in all copies.
91638Srgrimes *
101638Srgrimes * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
111638Srgrimes * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
121638Srgrimes * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
131638Srgrimes * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
141638Srgrimes * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
151638Srgrimes * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
161638Srgrimes * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
171638Srgrimes */
181638Srgrimes#include <sys/types.h>
191638Srgrimes#include <dev/ic/tivar.h>
201638Srgrimes#include <err.h>
211638Srgrimes#include <fcntl.h>
221638Srgrimes#include <stdio.h>
231638Srgrimes#include <stdlib.h>
241638Srgrimes#include <string.h>
251638Srgrimes#include <unistd.h>
261638Srgrimes
271638Srgrimes#include "ti_fw.h"
281638Srgrimes#include "ti_fw2.h"
291638Srgrimes
301638Srgrimesstatic void
311638Srgrimesoutput(const char *name,
321638Srgrimes    const int FwReleaseMajor, const int FwReleaseMinor,
331638Srgrimes    const int FwReleaseFix, const u_int32_t FwStartAddr,
34108533Sschweikh    const u_int32_t FwTextAddr, const int FwTextLen,
35108533Sschweikh    const u_int32_t FwRodataAddr, const int FwRodataLen,
361638Srgrimes    const u_int32_t FwDataAddr, const int FwDataLen,
371638Srgrimes    const u_int32_t FwSbssAddr, const int FwSbssLen,
381638Srgrimes    const u_int32_t FwBssAddr, const int FwBssLen,
391638Srgrimes    const u_int32_t *FwText, int sizetext,
401638Srgrimes    const u_int32_t *FwRodata, int sizerodata,
411638Srgrimes    const u_int32_t *FwData, int sizedata)
421638Srgrimes{
431638Srgrimes	struct	tigon_firmware tfproto, *tf;
441638Srgrimes	int len, fd, i, cnt;
451638Srgrimes	u_int32_t *b;
461638Srgrimes	ssize_t rlen;
471638Srgrimes
481638Srgrimes	len = sizeof(tfproto) - sizeof(tfproto.data) + sizetext +
491638Srgrimes	    sizerodata + sizedata;
501638Srgrimes	tf = (struct tigon_firmware *)malloc(len);
511638Srgrimes	bzero(tf, len);
521638Srgrimes
531638Srgrimes	tf->FwReleaseMajor = FwReleaseMajor;
541638Srgrimes	tf->FwReleaseMinor = FwReleaseMinor;
551638Srgrimes	tf->FwReleaseFix = FwReleaseFix;
561638Srgrimes	tf->FwStartAddr = FwStartAddr;
571638Srgrimes
581638Srgrimes	tf->FwTextAddr = FwTextAddr;
591638Srgrimes	tf->FwTextLen = FwTextLen;
601638Srgrimes
611638Srgrimes	tf->FwRodataAddr = FwRodataAddr;
621638Srgrimes	tf->FwRodataLen = FwRodataLen;
631638Srgrimes
641638Srgrimes	tf->FwDataAddr = FwDataAddr;
651638Srgrimes	tf->FwDataLen = FwDataLen;
661638Srgrimes
671638Srgrimes	tf->FwSbssAddr = FwSbssAddr;
681638Srgrimes	tf->FwSbssLen = FwSbssLen;
691638Srgrimes
701638Srgrimes	tf->FwBssAddr = FwBssAddr;
711638Srgrimes	tf->FwBssLen = FwBssLen;
721638Srgrimes
731638Srgrimes	tf->FwTextOffset = 0;
741638Srgrimes	tf->FwRodataOffset = sizetext;
751638Srgrimes	tf->FwDataOffset = sizetext + sizerodata;
761638Srgrimes
771638Srgrimes	bcopy(FwText, &tf->data[tf->FwTextOffset], FwTextLen);
781638Srgrimes	bcopy(FwRodata, &tf->data[tf->FwRodataOffset], FwRodataLen);
791638Srgrimes	bcopy(FwData, &tf->data[tf->FwDataOffset], FwDataLen);
801638Srgrimes
811638Srgrimes	b = (u_int32_t *)tf;
821638Srgrimes	cnt = len / sizeof(u_int32_t);
831638Srgrimes	for (i = 0; i < cnt; i++)
841638Srgrimes		 b[i] = htole32(b[i]);
851638Srgrimes
861638Srgrimes	printf("creating %s length %d [%d+%d+%d] [%d+%d+%d]\n",
871638Srgrimes	    name, len, FwTextLen, FwRodataLen, FwDataLen,
881638Srgrimes	    sizetext, sizerodata, sizedata);
891638Srgrimes	fd = open(name, O_WRONLY|O_CREAT|O_TRUNC, 0644);
901638Srgrimes	if (fd == -1)
911638Srgrimes		err(1, "%s", name);
921638Srgrimes
931638Srgrimes	rlen = write(fd, tf, len);
941638Srgrimes	if (rlen == -1)
951638Srgrimes		err(1, "%s", name);
961638Srgrimes	if (rlen != len)
971638Srgrimes		errx(1, "%s: short write", name);
981638Srgrimes	free(tf);
991638Srgrimes	close(fd);
1001638Srgrimes}
1011638Srgrimes
1021638Srgrimes
1031638Srgrimesint
1041638Srgrimesmain(int argc, char *argv[])
1051638Srgrimes{
1061638Srgrimes
1071638Srgrimes	output("tigon1",
1081638Srgrimes	    tigonFwReleaseMajor, tigonFwReleaseMinor,
1091638Srgrimes	    tigonFwReleaseFix, tigonFwStartAddr,
1101638Srgrimes	    tigonFwTextAddr, tigonFwTextLen,
1111638Srgrimes	    tigonFwRodataAddr, tigonFwRodataLen,
1121638Srgrimes	    tigonFwDataAddr, tigonFwDataLen,
1131638Srgrimes	    tigonFwSbssAddr, tigonFwSbssLen,
1141638Srgrimes	    tigonFwBssAddr, tigonFwBssLen,
1151638Srgrimes	    tigonFwText, sizeof tigonFwText,
1161638Srgrimes	    tigonFwRodata, sizeof tigonFwRodata,
1171638Srgrimes	    tigonFwData, sizeof tigonFwData);
1181638Srgrimes
1191638Srgrimes	output("tigon2",
1201638Srgrimes	    tigon2FwReleaseMajor, tigon2FwReleaseMinor,
1211638Srgrimes	    tigon2FwReleaseFix, tigon2FwStartAddr,
1221638Srgrimes	    tigon2FwTextAddr, tigon2FwTextLen,
1231638Srgrimes	    tigon2FwRodataAddr, tigon2FwRodataLen,
1241638Srgrimes	    tigon2FwDataAddr, tigon2FwDataLen,
1251638Srgrimes	    tigon2FwSbssAddr, tigon2FwSbssLen,
1261638Srgrimes	    tigon2FwBssAddr, tigon2FwBssLen,
1271638Srgrimes	    tigon2FwText, sizeof tigon2FwText,
1281638Srgrimes	    tigon2FwRodata, sizeof tigon2FwRodata,
1291638Srgrimes	    tigon2FwData, sizeof tigon2FwData);
1301638Srgrimes
1311638Srgrimes	return 0;
1321638Srgrimes}
1331638Srgrimes