2 * A simple firmware helper program.
4 * Copyright 2005 Red Hat, Inc.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation version 2 of the License.
20 #include "../../udev_utils.h"
21 #include "../../logging.h"
23 #define FIRMWARE_PATH "/lib/firmware"
27 void log_message(int priority, const char *format, ...)
30 static int udev_log = -1;
35 value = getenv("UDEV_LOG");
37 udev_log = log_priority(value);
42 if (priority > udev_log)
45 va_start(args, format);
46 vsyslog(priority, format, args);
51 /* Set the 'loading' attribute for a firmware device.
52 * 1 == currently loading
56 static int set_loading(const char *device, int value) {
57 char loading_path[PATH_SIZE];
61 snprintf(loading_path, sizeof(loading_path), "/sys/%s/loading", device);
62 loading_path[sizeof(loading_path)-1] = '\0';
63 f = fopen(loading_path, "w");
66 rc = fprintf(f, "%d", value);
73 int main(int argc, char **argv) {
74 char *devpath, *firmware, *action, *driver;
75 char fw_path[PATH_SIZE];
76 char data_path[PATH_SIZE];
79 size_t fw_buffer_size;
83 logging_init("firmware_helper");
85 driver = getenv("PHYSDEVDRIVER");
88 devpath = getenv("DEVPATH");
89 firmware = getenv("FIRMWARE");
90 action = getenv("ACTION");
91 if (!devpath || !firmware || !action || strcmp(action,"add") != 0) {
92 err("missing devpath, action or firmware");
96 dbg("try to load firmware '%s' for '%s'", firmware, devpath);
97 set_loading(devpath, 1);
99 snprintf(fw_path, sizeof(fw_path), "%s/%s", FIRMWARE_PATH, firmware);
100 fw_path[sizeof(fw_path)-1] = '\0';
101 if (file_map(fw_path, &fw_buffer, &fw_buffer_size) != 0 || fw_buffer_size == 0) {
102 err("could not load firmware '%s' for '%s'", fw_path, devpath);
107 snprintf(data_path, sizeof(data_path), "/sys/%s/data", devpath);
108 data_path[sizeof(data_path)-1] = '\0';
109 fw_fd = open(data_path, O_RDWR);
116 while (count < fw_buffer_size) {
119 c = write(fw_fd, fw_buffer+count, fw_buffer_size-count);
129 file_unmap(fw_buffer, fw_buffer_size);
130 set_loading(devpath, 0);
131 info("loaded '%s' for device '%s'", fw_path, devpath);
137 file_unmap(fw_buffer, fw_buffer_size);
138 set_loading(devpath, -1);
140 err("error loading '%s' for device '%s' with driver '%s'", fw_path, devpath, driver);