chiark / gitweb /
36ce79054c2a1c788e700611d9ee3feba087f885
[elogind.git] / extras / firmware / firmware.c
1 /*
2  * firmware - Load firmware device
3  *
4  * Copyright (C) 2009 Piter Punk <piterpunk@slackware.com>
5  * Copyright (C) 2009 Kay Sievers <kay.sievers@vrfy.org>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details:*
16  */
17
18 #include <unistd.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <stdio.h>
22 #include <getopt.h>
23 #include <errno.h>
24 #include <stdbool.h>
25 #include <sys/utsname.h>
26 #include <sys/stat.h>
27 #include "libudev-private.h"
28
29 static bool set_loading(struct udev *udev, char *loadpath, const char *state)
30 {
31         FILE *ldfile;
32
33         ldfile = fopen(loadpath, "w");
34         if (ldfile == NULL) {
35                 err(udev, "error: can not open '%s'\n", loadpath);
36                 return false;
37         };
38         fprintf(ldfile, "%s\n", state);
39         fclose(ldfile);
40         return true;
41 }
42
43 static bool copy_firmware(struct udev *udev, const char *source, const char *target, size_t size)
44 {
45         char *buf;
46         FILE *fsource = NULL, *ftarget = NULL;
47         bool ret = false;
48
49         buf = malloc(size);
50         if (buf == NULL) {
51                 err(udev,"No memory available to load firmware file");
52                 return false;
53         }
54
55         fsource = fopen(source, "r");
56         if (fsource == NULL)
57                 goto exit;
58         ftarget = fopen(target, "w");
59         if (ftarget == NULL)
60                 goto exit;
61         if (fread(buf, size, 1, fsource) != 1)
62                 goto exit;
63         if (fwrite(buf, size, 1, ftarget) == 1)
64                 ret = true;
65 exit:
66         if (ftarget != NULL)
67                 fclose(ftarget);
68         if (fsource != NULL)
69                 fclose(fsource);
70         free(buf);
71         return ret;
72 }
73
74 int main(int argc, char **argv)
75 {
76         static const struct option options[] = {
77                 { "firmware", required_argument, NULL, 'f' },
78                 { "devpath", required_argument, NULL, 'p' },
79                 { "help", no_argument, NULL, 'h' },
80                 {}
81         };
82         static const char *searchpath[] = { FIRMWARE_PATH };
83         char fwencpath[UTIL_PATH_SIZE];
84         char misspath[UTIL_PATH_SIZE];
85         char loadpath[UTIL_PATH_SIZE];
86         char datapath[UTIL_PATH_SIZE];
87         char fwpath[UTIL_PATH_SIZE];
88         char *devpath = NULL;
89         char *firmware = NULL;
90         FILE *fwfile;
91         struct utsname kernel;
92         struct stat statbuf;
93         struct udev *udev = NULL;
94         unsigned int i;
95         int rc = 0;
96
97         udev_log_init("firmware");
98
99         for (;;) {
100                 int option;
101
102                 option = getopt_long(argc, argv, "f:p:h", options, NULL);
103                 if (option == -1)
104                         break;
105
106                 switch (option) {
107                 case 'f':
108                         firmware = optarg;
109                         break;
110                 case 'p':
111                         devpath = optarg;
112                         break;
113                 case 'h':
114                         printf("Usage: firmware --firmware=<fwfile> --devpath=<path> [--help]\n\n");
115                         goto exit;
116                 }
117         }
118
119         if (devpath == NULL || firmware == NULL) {
120                 fprintf(stderr, "firmware or devpath parameter missing\n\n");
121                 rc = 1;
122                 goto exit;
123         }
124
125         udev = udev_new();
126         if (udev == NULL) {
127                 rc = 1;
128                 goto exit;
129         };
130
131         /* lookup firmware file */
132         uname(&kernel);
133         for (i = 0; i < ARRAY_SIZE(searchpath); i++) {
134                 util_strscpyl(fwpath, sizeof(fwpath), searchpath[i], kernel.release, "/", firmware, NULL);
135                 dbg(udev, "trying %s\n", fwpath);
136                 fwfile = fopen(fwpath, "r");
137                 if (fwfile != NULL)
138                         break; 
139
140                 util_strscpyl(fwpath, sizeof(fwpath), searchpath[i], firmware, NULL);
141                 dbg(udev, "trying %s\n", fwpath);
142                 fwfile = fopen(fwpath, "r");
143                 if (fwfile != NULL)
144                         break;
145         }
146
147         util_path_encode(firmware, fwencpath, sizeof(fwencpath));
148         util_strscpyl(misspath, sizeof(misspath), udev_get_run_path(udev), "/firmware-missing/", fwencpath, NULL);
149         util_strscpyl(loadpath, sizeof(loadpath), udev_get_sys_path(udev), devpath, "/loading", NULL);
150
151         if (fwfile == NULL) {
152                 int err;
153
154                 /* This link indicates the missing firmware file and the associated device */
155                 info(udev, "did not find firmware file '%s'\n", firmware);
156                 do {
157                         err = util_create_path(udev, misspath);
158                         if (err != 0 && err != -ENOENT)
159                                 break;
160                         err = symlink(devpath, misspath);
161                         if (err != 0)
162                                 err = -errno;
163                 } while (err == -ENOENT);
164                 rc = 2;
165                 set_loading(udev, loadpath, "-1");
166                 goto exit;
167         }
168
169         if (stat(fwpath, &statbuf) < 0 || statbuf.st_size == 0) {
170                 rc = 3;
171                 goto exit;
172         }
173         if (unlink(misspath) == 0)
174                 util_delete_path(udev, misspath);
175
176         if (!set_loading(udev, loadpath, "1"))
177                 goto exit;
178
179         util_strscpyl(datapath, sizeof(datapath), udev_get_sys_path(udev), devpath, "/data", NULL);
180         if (!copy_firmware(udev, fwpath, datapath, statbuf.st_size)) {
181                 err(udev, "error sending firmware '%s' to device\n", firmware);
182                 set_loading(udev, loadpath, "-1");
183                 rc = 4;
184                 goto exit;
185         };
186
187         set_loading(udev, loadpath, "0");
188 exit:
189         udev_unref(udev);
190         udev_log_close();
191         return rc;
192 }