1 /* $OpenBSD: build.c,v 1.8 2017/08/28 05:46:44 otto Exp $ */
2
3 /*
4 * Copyright (c) 2004 Theo de Raadt <deraadt@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 #include <sys/types.h>
19 #include <dev/pci/ydsvar.h>
20 #include <fcntl.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <err.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include "yds_hwmcode.h"
27
28 #define FILENAME "yds"
29
30 void
hswapn(u_int32_t * p,int wcount)31 hswapn(u_int32_t *p, int wcount)
32 {
33 for (; wcount; wcount -=4) {
34 *p = htonl(*p);
35 p++;
36 }
37 }
38
39 int
main(int argc,char * argv[])40 main(int argc, char *argv[])
41 {
42 struct yds_firmware yfproto, *yf;
43 int len, fd;
44 ssize_t rlen;
45
46 len = sizeof(*yf) - sizeof(yfproto.data) +
47 sizeof(yds_dsp_mcode) + sizeof(yds_ds1_ctrl_mcode) +
48 sizeof(yds_ds1e_ctrl_mcode);
49
50 yf = (struct yds_firmware *)malloc(len);
51 bzero(yf, len);
52
53 yf->dsplen = htonl(sizeof(yds_dsp_mcode));
54 yf->ds1len = htonl(sizeof(yds_ds1_ctrl_mcode));
55 yf->ds1elen = htonl(sizeof(yds_ds1e_ctrl_mcode));
56
57 bcopy(yds_dsp_mcode, &yf->data[0], sizeof(yds_dsp_mcode));
58 hswapn((u_int32_t *)&yf->data[0], sizeof(yds_dsp_mcode));
59
60 bcopy(yds_ds1_ctrl_mcode, &yf->data[0] + sizeof(yds_dsp_mcode),
61 sizeof(yds_ds1_ctrl_mcode));
62 hswapn((u_int32_t *)&yf->data[sizeof(yds_dsp_mcode)],
63 sizeof(yds_ds1_ctrl_mcode));
64
65 bcopy(yds_ds1e_ctrl_mcode,
66 &yf->data[0] + sizeof(yds_dsp_mcode) + sizeof(yds_ds1_ctrl_mcode),
67 sizeof(yds_ds1e_ctrl_mcode));
68 hswapn((u_int32_t *)&yf->data[sizeof(yds_dsp_mcode) +
69 sizeof(yds_ds1_ctrl_mcode)],
70 sizeof(yds_ds1e_ctrl_mcode));
71
72 printf("creating %s length %d [%zu+%zu+%zu]\n",
73 FILENAME, len, sizeof(yds_dsp_mcode),
74 sizeof(yds_ds1_ctrl_mcode), sizeof(yds_ds1e_ctrl_mcode));
75 fd = open(FILENAME, O_WRONLY|O_CREAT|O_TRUNC, 0644);
76 if (fd == -1)
77 err(1, FILENAME);
78
79 rlen = write(fd, yf, len);
80 if (rlen == -1)
81 err(1, "%s", FILENAME);
82 if (rlen != len)
83 errx(1, "%s: short write", FILENAME);
84 free(yf);
85 close(fd);
86 return 0;
87 }
88