1*38bad194Sotto /* $OpenBSD: build.c,v 1.8 2017/08/28 05:46:44 otto Exp $ */
265b5a2beSderaadt
365b5a2beSderaadt /*
465b5a2beSderaadt * Copyright (c) 2004 Theo de Raadt <deraadt@openbsd.org>
565b5a2beSderaadt *
665b5a2beSderaadt * Permission to use, copy, modify, and distribute this software for any
765b5a2beSderaadt * purpose with or without fee is hereby granted, provided that the above
865b5a2beSderaadt * copyright notice and this permission notice appear in all copies.
965b5a2beSderaadt *
1065b5a2beSderaadt * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1165b5a2beSderaadt * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1265b5a2beSderaadt * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1365b5a2beSderaadt * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1465b5a2beSderaadt * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1565b5a2beSderaadt * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1665b5a2beSderaadt * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1765b5a2beSderaadt */
1865b5a2beSderaadt #include <sys/types.h>
1965b5a2beSderaadt #include <dev/pci/ydsvar.h>
2065b5a2beSderaadt #include <fcntl.h>
2165b5a2beSderaadt #include <stdlib.h>
22948e15b6Sjason #include <unistd.h>
23948e15b6Sjason #include <err.h>
24948e15b6Sjason #include <string.h>
25948e15b6Sjason #include <stdio.h>
2665b5a2beSderaadt #include "yds_hwmcode.h"
2765b5a2beSderaadt
2865b5a2beSderaadt #define FILENAME "yds"
2965b5a2beSderaadt
30fc31a2a7Sderaadt void
hswapn(u_int32_t * p,int wcount)31fc31a2a7Sderaadt hswapn(u_int32_t *p, int wcount)
32fc31a2a7Sderaadt {
33fc31a2a7Sderaadt for (; wcount; wcount -=4) {
34fc31a2a7Sderaadt *p = htonl(*p);
35fc31a2a7Sderaadt p++;
36fc31a2a7Sderaadt }
37fc31a2a7Sderaadt }
38fc31a2a7Sderaadt
3965b5a2beSderaadt int
main(int argc,char * argv[])4065b5a2beSderaadt main(int argc, char *argv[])
4165b5a2beSderaadt {
4265b5a2beSderaadt struct yds_firmware yfproto, *yf;
43948e15b6Sjason int len, fd;
44948e15b6Sjason ssize_t rlen;
4565b5a2beSderaadt
4665b5a2beSderaadt len = sizeof(*yf) - sizeof(yfproto.data) +
4765b5a2beSderaadt sizeof(yds_dsp_mcode) + sizeof(yds_ds1_ctrl_mcode) +
4865b5a2beSderaadt sizeof(yds_ds1e_ctrl_mcode);
4965b5a2beSderaadt
5065b5a2beSderaadt yf = (struct yds_firmware *)malloc(len);
5165b5a2beSderaadt bzero(yf, len);
5265b5a2beSderaadt
53fc31a2a7Sderaadt yf->dsplen = htonl(sizeof(yds_dsp_mcode));
54fc31a2a7Sderaadt yf->ds1len = htonl(sizeof(yds_ds1_ctrl_mcode));
55fc31a2a7Sderaadt yf->ds1elen = htonl(sizeof(yds_ds1e_ctrl_mcode));
5665b5a2beSderaadt
57fc31a2a7Sderaadt bcopy(yds_dsp_mcode, &yf->data[0], sizeof(yds_dsp_mcode));
58fc31a2a7Sderaadt hswapn((u_int32_t *)&yf->data[0], sizeof(yds_dsp_mcode));
59fc31a2a7Sderaadt
60*38bad194Sotto bcopy(yds_ds1_ctrl_mcode, &yf->data[0] + sizeof(yds_dsp_mcode),
61fc31a2a7Sderaadt sizeof(yds_ds1_ctrl_mcode));
62fc31a2a7Sderaadt hswapn((u_int32_t *)&yf->data[sizeof(yds_dsp_mcode)],
63fc31a2a7Sderaadt sizeof(yds_ds1_ctrl_mcode));
64fc31a2a7Sderaadt
65fc31a2a7Sderaadt bcopy(yds_ds1e_ctrl_mcode,
66*38bad194Sotto &yf->data[0] + sizeof(yds_dsp_mcode) + sizeof(yds_ds1_ctrl_mcode),
67fc31a2a7Sderaadt sizeof(yds_ds1e_ctrl_mcode));
68fc31a2a7Sderaadt hswapn((u_int32_t *)&yf->data[sizeof(yds_dsp_mcode) +
69fc31a2a7Sderaadt sizeof(yds_ds1_ctrl_mcode)],
70fc31a2a7Sderaadt sizeof(yds_ds1e_ctrl_mcode));
7165b5a2beSderaadt
72867e0126Skrw printf("creating %s length %d [%zu+%zu+%zu]\n",
73fc31a2a7Sderaadt FILENAME, len, sizeof(yds_dsp_mcode),
74fc31a2a7Sderaadt sizeof(yds_ds1_ctrl_mcode), sizeof(yds_ds1e_ctrl_mcode));
7565b5a2beSderaadt fd = open(FILENAME, O_WRONLY|O_CREAT|O_TRUNC, 0644);
7665b5a2beSderaadt if (fd == -1)
7765b5a2beSderaadt err(1, FILENAME);
7865b5a2beSderaadt
79948e15b6Sjason rlen = write(fd, yf, len);
80948e15b6Sjason if (rlen == -1)
81948e15b6Sjason err(1, "%s", FILENAME);
82948e15b6Sjason if (rlen != len)
83948e15b6Sjason errx(1, "%s: short write", FILENAME);
844c67e3eeStedu free(yf);
8565b5a2beSderaadt close(fd);
8665b5a2beSderaadt return 0;
8765b5a2beSderaadt }
88