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, *ftarget;
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)
72 int main(int argc, char **argv)
74 static const struct option options[] = {
75 { "firmware", required_argument, NULL, 'f' },
76 { "devpath", required_argument, NULL, 'p' },
77 { "help", no_argument, NULL, 'h' },
80 static const char *searchpath[] = {
81 "/lib/firmware/updates/",
84 char fwencpath[UTIL_PATH_SIZE];
85 char misspath[UTIL_PATH_SIZE];
86 char loadpath[UTIL_PATH_SIZE];
87 char datapath[UTIL_PATH_SIZE];
88 char fwpath[UTIL_PATH_SIZE];
90 char *firmware = NULL;
92 struct utsname kernel;
94 struct udev *udev = NULL;
98 udev_log_init("firmware");
103 option = getopt_long(argc, argv, "f:p:h", options, NULL);
115 printf("Usage: firmware --firmware=<fwfile> --devpath=<path> [--help]\n\n");
122 if (devpath == NULL || firmware == NULL) {
123 fprintf(stderr, "firmware or devpath parameter missing\n\n");
134 /* lookup firmware file */
136 for (i = 0; i < ARRAY_SIZE(searchpath); i++) {
137 util_strscpyl(fwpath, sizeof(fwpath), searchpath[i], kernel.release, "/", firmware, NULL);
138 dbg(udev, "trying %s\n", fwpath);
139 fwfile = fopen(fwpath, "r");
143 util_strscpyl(fwpath, sizeof(fwpath), searchpath[i], firmware, NULL);
144 dbg(udev, "trying %s\n", fwpath);
145 fwfile = fopen(fwpath, "r");
150 util_path_encode(firmware, fwencpath, sizeof(fwencpath));
151 util_strscpyl(misspath, sizeof(misspath), udev_get_dev_path(udev), "/.udev/firmware-missing/", fwencpath, NULL);
153 if (fwfile == NULL) {
156 /* This link indicates the missing firmware file and the associated device */
157 info(udev, "did not find firmware file '%s'\n", firmware);
159 err = util_create_path(udev, misspath);
160 if (err != 0 && err != -ENOENT)
162 udev_selinux_setfscreatecon(udev, misspath, S_IFLNK);
163 err = symlink(devpath, misspath);
166 udev_selinux_resetfscreatecon(udev);
167 } while (err == -ENOENT);
172 if (stat(fwpath, &statbuf) < 0 || statbuf.st_size == 0) {
176 if (unlink(misspath) == 0)
177 util_delete_path(udev, misspath);
179 util_strscpyl(loadpath, sizeof(loadpath), udev_get_sys_path(udev), devpath, "/loading", NULL);
180 set_loading(udev, loadpath, "1");
182 util_strscpyl(datapath, sizeof(datapath), udev_get_sys_path(udev), devpath, "/data", NULL);
183 if (!copy_firmware(udev, fwpath, datapath, statbuf.st_size)) {
184 err(udev, "error sending firmware '%s' to device\n", firmware);
185 set_loading(udev, loadpath, "-1");
190 set_loading(udev, loadpath, "0");