Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / usr.sbin / isdn / daicctl / dnload.c
blobf5b5cbc17c6041b7347244c8ca3bf04815bd60e9
1 /* $NetBSD: dnload.c,v 1.6 2008/04/28 20:24:16 martin Exp $ */
3 /*-
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Martin Husemann <martin@NetBSD.org>.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <errno.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <sys/ioctl.h>
39 #include <netisdn/i4b_ioctl.h>
40 #include "daicctl.h"
42 void
43 download(fd, controller, filename)
44 int fd, controller;
45 char *filename;
47 int i, num_ports = 1;
48 struct isdn_download_request dr;
49 struct isdn_dr_prot prots[4];
50 msg_ctrl_info_req_t info;
51 FILE *f;
52 struct stat sb;
53 u_int8_t *data;
54 size_t rlen;
56 memset(&info, 0, sizeof info);
57 info.controller = controller;
58 if (ioctl(fd, I4B_CTRL_INFO_REQ, &info) == -1) {
59 perror("ctrl info req");
60 exit(1);
62 if (strncmp(info.devname, "daic", 4) != 0) {
63 fprintf(stderr, "this is not a Diehl active isdn card...\n");
64 exit(1);
66 if (strcmp(info.devname, "daic0 port0") == 0)
67 num_ports = 4;
69 memset(&dr, 0, sizeof dr);
70 dr.controller = controller;
71 dr.numprotos = num_ports;
72 dr.protocols = prots;
73 if (stat(filename, &sb)) {
74 perror(filename);
75 exit(1);
77 f = fopen(filename, "r");
78 if (!f) {
79 perror(filename);
80 exit(1);
82 data = (u_int8_t*)malloc(sb.st_size);
83 if (!data) {
84 fprintf(stderr, "could not allocate memory for microcode!\n");
85 exit(1);
87 rlen = fread(data, 1, sb.st_size, f);
88 if ((off_t)rlen != sb.st_size) {
89 fprintf(stderr, "error reading microcode, read %lu bytes: %s\n",
90 (unsigned long)rlen, strerror(errno));
91 exit(1);
93 fclose(f);
94 for (i = 0; i < num_ports; i++) {
95 prots[i].bytecount = sb.st_size;
96 prots[i].microcode = data;
98 if (ioctl(fd, I4B_CTRL_DOWNLOAD, &dr) == -1) {
99 fprintf(stderr, "downloading microcode to controller %d failed: %s\n",
100 controller, strerror(errno));
101 exit(1);
103 printf("download successful\n");