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.h"
22 #define FIRMWARE_PATH "/lib/firmware"
26 void log_message(int priority, const char *format, ...)
29 static int udev_log = -1;
34 value = getenv("UDEV_LOG");
36 udev_log = log_priority(value);
41 if (priority > udev_log)
44 va_start(args, format);
45 vsyslog(priority, format, args);
50 /* Set the 'loading' attribute for a firmware device.
51 * 1 == currently loading
55 static int set_loading(const char *device, int value) {
56 char loading_path[PATH_SIZE];
60 snprintf(loading_path, sizeof(loading_path), "/sys/%s/loading", device);
61 loading_path[sizeof(loading_path)-1] = '\0';
62 f = fopen(loading_path, "w");
65 rc = fprintf(f, "%d", value);
72 int main(int argc, char **argv) {
73 char *devpath, *firmware, *action, *driver;
74 char fw_path[PATH_SIZE];
75 char data_path[PATH_SIZE];
78 size_t fw_buffer_size;
82 logging_init("firmware_helper");
84 driver = getenv("PHYSDEVDRIVER");
87 devpath = getenv("DEVPATH");
88 firmware = getenv("FIRMWARE");
89 action = getenv("ACTION");
90 if (!devpath || !firmware || !action || strcmp(action,"add") != 0) {
91 err("missing devpath, action or firmware");
95 dbg("try to load firmware '%s' for '%s'", firmware, devpath);
96 set_loading(devpath, 1);
98 snprintf(fw_path, sizeof(fw_path), "%s/%s", FIRMWARE_PATH, firmware);
99 fw_path[sizeof(fw_path)-1] = '\0';
100 if (file_map(fw_path, &fw_buffer, &fw_buffer_size) != 0 || fw_buffer_size == 0) {
101 err("could not load firmware '%s' for '%s'", fw_path, devpath);
106 snprintf(data_path, sizeof(data_path), "/sys/%s/data", devpath);
107 data_path[sizeof(data_path)-1] = '\0';
108 fw_fd = open(data_path, O_RDWR);
115 while (count < fw_buffer_size) {
118 c = write(fw_fd, fw_buffer+count, fw_buffer_size-count);
128 file_unmap(fw_buffer, fw_buffer_size);
129 set_loading(devpath, 0);
130 info("loaded '%s' for device '%s'", fw_path, devpath);
136 file_unmap(fw_buffer, fw_buffer_size);
137 set_loading(devpath, -1);
139 err("error loading '%s' for device '%s' with driver '%s'", fw_path, devpath, driver);