2 * firmware - Load firmware device
4 * Copyright (C) 2009 Piter Punk <piterpunk@slackware.com>
5 * Copyright (C) 2009 Kay Sievers <kay.sievers@vrfy.org>
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.
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:*
25 #include <sys/utsname.h>
27 #include "libudev-private.h"
29 static bool set_loading(struct udev *udev, char *loadpath, const char *state)
33 ldfile = fopen(loadpath, "w");
35 err(udev, "error: can not open '%s'\n", loadpath);
38 fprintf(ldfile, "%s\n", state);
43 static bool copy_firmware(struct udev *udev, const char *source, const char *target, size_t size)
46 FILE *fsource = NULL, *ftarget = NULL;
51 err(udev,"No memory available to load firmware file");
55 fsource = fopen(source, "r");
58 ftarget = fopen(target, "w");
61 if (fread(buf, size, 1, fsource) != 1)
63 if (fwrite(buf, size, 1, ftarget) == 1)
74 int main(int argc, char **argv)
76 static const struct option options[] = {
77 { "firmware", required_argument, NULL, 'f' },
78 { "devpath", required_argument, NULL, 'p' },
79 { "help", no_argument, NULL, 'h' },
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];
89 char *firmware = NULL;
91 struct utsname kernel;
93 struct udev *udev = NULL;
97 udev_log_init("firmware");
102 option = getopt_long(argc, argv, "f:p:h", options, NULL);
114 printf("Usage: firmware --firmware=<fwfile> --devpath=<path> [--help]\n\n");
119 if (devpath == NULL || firmware == NULL) {
120 fprintf(stderr, "firmware or devpath parameter missing\n\n");
131 /* lookup firmware file */
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");
140 util_strscpyl(fwpath, sizeof(fwpath), searchpath[i], firmware, NULL);
141 dbg(udev, "trying %s\n", fwpath);
142 fwfile = fopen(fwpath, "r");
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);
151 if (fwfile == NULL) {
154 /* This link indicates the missing firmware file and the associated device */
155 info(udev, "did not find firmware file '%s'\n", firmware);
157 err = util_create_path(udev, misspath);
158 if (err != 0 && err != -ENOENT)
160 err = symlink(devpath, misspath);
163 } while (err == -ENOENT);
165 set_loading(udev, loadpath, "-1");
169 if (stat(fwpath, &statbuf) < 0 || statbuf.st_size == 0) {
173 if (unlink(misspath) == 0)
174 util_delete_path(udev, misspath);
176 if (!set_loading(udev, loadpath, "1"))
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");
187 set_loading(udev, loadpath, "0");