2 * firmware - Kernel firmware loader
4 * Copyright (C) 2009 Piter Punk <piterpunk@slackware.com>
5 * Copyright (C) 2009-2011 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>
30 static bool set_loading(struct udev *udev, char *loadpath, const char *state)
34 ldfile = fopen(loadpath, "we");
36 err(udev, "error: can not open '%s'\n", loadpath);
39 fprintf(ldfile, "%s\n", state);
44 static bool copy_firmware(struct udev *udev, const char *source, const char *target, size_t size)
47 FILE *fsource = NULL, *ftarget = NULL;
52 err(udev,"No memory available to load firmware file");
56 info(udev, "writing '%s' (%zi) to '%s'\n", source, size, target);
58 fsource = fopen(source, "re");
61 ftarget = fopen(target, "we");
64 if (fread(buf, size, 1, fsource) != 1)
66 if (fwrite(buf, size, 1, ftarget) == 1)
77 static int builtin_firmware(struct udev_device *dev, int argc, char *argv[], bool test)
79 struct udev *udev = udev_device_get_udev(dev);
80 static const char *searchpath[] = { FIRMWARE_PATH };
81 char fwencpath[UTIL_PATH_SIZE];
82 char misspath[UTIL_PATH_SIZE];
83 char loadpath[UTIL_PATH_SIZE];
84 char datapath[UTIL_PATH_SIZE];
85 char fwpath[UTIL_PATH_SIZE];
88 struct utsname kernel;
91 int rc = EXIT_SUCCESS;
93 firmware = udev_device_get_property_value(dev, "FIRMWARE");
94 if (firmware == NULL) {
95 err(udev, "firmware parameter missing\n\n");
100 /* lookup firmware file */
102 for (i = 0; i < ARRAY_SIZE(searchpath); i++) {
103 util_strscpyl(fwpath, sizeof(fwpath), searchpath[i], kernel.release, "/", firmware, NULL);
104 dbg(udev, "trying %s\n", fwpath);
105 fwfile = fopen(fwpath, "re");
109 util_strscpyl(fwpath, sizeof(fwpath), searchpath[i], firmware, NULL);
110 dbg(udev, "trying %s\n", fwpath);
111 fwfile = fopen(fwpath, "re");
116 util_path_encode(firmware, fwencpath, sizeof(fwencpath));
117 util_strscpyl(misspath, sizeof(misspath), udev_get_run_path(udev), "/firmware-missing/", fwencpath, NULL);
118 util_strscpyl(loadpath, sizeof(loadpath), udev_device_get_syspath(dev), "/loading", NULL);
120 if (fwfile == NULL) {
123 /* This link indicates the missing firmware file and the associated device */
124 info(udev, "did not find firmware file '%s'\n", firmware);
126 err = util_create_path(udev, misspath);
127 if (err != 0 && err != -ENOENT)
129 err = symlink(udev_device_get_devpath(dev), misspath);
132 } while (err == -ENOENT);
134 set_loading(udev, loadpath, "-1");
138 if (stat(fwpath, &statbuf) < 0 || statbuf.st_size == 0) {
142 if (unlink(misspath) == 0)
143 util_delete_path(udev, misspath);
145 if (!set_loading(udev, loadpath, "1"))
148 util_strscpyl(datapath, sizeof(datapath), udev_device_get_syspath(dev), "/data", NULL);
149 if (!copy_firmware(udev, fwpath, datapath, statbuf.st_size)) {
150 err(udev, "error sending firmware '%s' to device\n", firmware);
151 set_loading(udev, loadpath, "-1");
156 set_loading(udev, loadpath, "0");
163 const struct udev_builtin udev_builtin_firmware = {
165 .cmd = builtin_firmware,
166 .help = "kernel firmware loader",