tizen 2.4 release
[external/systemd.git] / src / udev / udev-builtin-firmware.c
1 /*
2  * firmware - Kernel firmware loader
3  *
4  * Copyright (C) 2009 Piter Punk <piterpunk@slackware.com>
5  * Copyright (C) 2009-2011 Kay Sievers <kay@vrfy.org>
6  *
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.
11  *
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:*
16  */
17
18 #include <unistd.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <stdio.h>
22 #include <getopt.h>
23 #include <errno.h>
24 #include <stdbool.h>
25 #include <sys/utsname.h>
26 #include <sys/stat.h>
27
28 #include "udev.h"
29
30 static bool set_loading(struct udev *udev, char *loadpath, const char *state) {
31         FILE *ldfile;
32
33         ldfile = fopen(loadpath, "we");
34         if (ldfile == NULL) {
35                 log_error("error: can not open '%s'", loadpath);
36                 return false;
37         };
38         fprintf(ldfile, "%s\n", state);
39         fclose(ldfile);
40         return true;
41 }
42
43 static bool copy_firmware(struct udev *udev, const char *source, const char *target, size_t size) {
44         char *buf;
45         FILE *fsource = NULL, *ftarget = NULL;
46         bool ret = false;
47
48         buf = malloc(size);
49         if (buf == NULL) {
50                 log_error("No memory available to load firmware file");
51                 return false;
52         }
53
54         log_debug("writing '%s' (%zi) to '%s'", source, size, target);
55
56         fsource = fopen(source, "re");
57         if (fsource == NULL)
58                 goto exit;
59         ftarget = fopen(target, "we");
60         if (ftarget == NULL)
61                 goto exit;
62         if (fread(buf, size, 1, fsource) != 1)
63                 goto exit;
64         if (fwrite(buf, size, 1, ftarget) == 1)
65                 ret = true;
66 exit:
67         if (ftarget != NULL)
68                 fclose(ftarget);
69         if (fsource != NULL)
70                 fclose(fsource);
71         free(buf);
72         return ret;
73 }
74
75 #ifdef CONFIG_TIZEN
76 static FILE* find_firmware(const char *path, const char *fwpath, const char *firmware)
77 {
78         char searchpath[UTIL_PATH_SIZE];
79         FILE *fwfile = NULL;
80         DIR *srcdir;
81         struct dirent *dent;
82         struct stat statbuf;
83
84         srcdir = opendir(path);
85         while ((dent = readdir(srcdir)) != NULL) {
86                 if (strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0)
87                         continue;
88                 if (fstatat(dirfd(srcdir), dent->d_name, &statbuf, 0) < 0)
89                         continue;
90                 if (S_ISDIR(statbuf.st_mode)) {
91                         strscpyl(fwpath, UTIL_PATH_SIZE, path, dent->d_name, "/", firmware, NULL);
92                         fwfile = fopen(fwpath, "re");
93                         if (fwfile == NULL) {
94                                 strscpyl(searchpath, UTIL_PATH_SIZE, path, dent->d_name, "/", NULL);
95                                 fwfile = find_firmware(searchpath, fwpath, firmware);
96                         }
97                         if (fwfile != NULL)
98                                 return fwfile;
99                 }
100         }
101         return NULL;
102 }
103 #endif
104
105 static int builtin_firmware(struct udev_device *dev, int argc, char *argv[], bool test) {
106         struct udev *udev = udev_device_get_udev(dev);
107         static const char *searchpath[] = { FIRMWARE_PATH };
108         char loadpath[UTIL_PATH_SIZE];
109         char datapath[UTIL_PATH_SIZE];
110         char fwpath[UTIL_PATH_SIZE];
111         const char *firmware;
112         FILE *fwfile = NULL;
113         struct utsname kernel;
114         struct stat statbuf;
115         unsigned int i;
116         int rc = EXIT_SUCCESS;
117
118         firmware = udev_device_get_property_value(dev, "FIRMWARE");
119         if (firmware == NULL) {
120                 log_error("firmware parameter missing");
121                 rc = EXIT_FAILURE;
122                 goto exit;
123         }
124
125         /* lookup firmware file */
126         uname(&kernel);
127         for (i = 0; i < ELEMENTSOF(searchpath); i++) {
128                 strscpyl(fwpath, sizeof(fwpath), searchpath[i], kernel.release, "/", firmware, NULL);
129                 fwfile = fopen(fwpath, "re");
130                 if (fwfile != NULL)
131                         break;
132
133                 strscpyl(fwpath, sizeof(fwpath), searchpath[i], firmware, NULL);
134                 fwfile = fopen(fwpath, "re");
135                 if (fwfile != NULL)
136                         break;
137         }
138
139 #ifdef CONFIG_TIZEN
140         /* If not found, try to find firmware in sub-directories. */
141         if (fwfile == NULL) {
142                 for (i = 0; i < ELEMENTSOF(searchpath); i++) {
143                         fwfile = find_firmware(searchpath[i], fwpath, firmware);
144                         if (fwfile != NULL)
145                                 break;
146                 }
147         }
148 #endif
149
150         strscpyl(loadpath, sizeof(loadpath), udev_device_get_syspath(dev), "/loading", NULL);
151
152         if (fwfile == NULL) {
153                 log_debug("did not find firmware file '%s'", firmware);
154                 rc = EXIT_FAILURE;
155                 /*
156                  * Do not cancel the request in the initrd, the real root might have
157                  * the firmware file and the 'coldplug' run in the real root will find
158                  * this pending request and fulfill or cancel it.
159                  * */
160                 if (!in_initrd())
161                         set_loading(udev, loadpath, "-1");
162                 goto exit;
163         }
164
165         if (stat(fwpath, &statbuf) < 0 || statbuf.st_size == 0) {
166                 if (!in_initrd())
167                         set_loading(udev, loadpath, "-1");
168                 rc = EXIT_FAILURE;
169                 goto exit;
170         }
171
172         if (!set_loading(udev, loadpath, "1"))
173                 goto exit;
174
175         strscpyl(datapath, sizeof(datapath), udev_device_get_syspath(dev), "/data", NULL);
176         if (!copy_firmware(udev, fwpath, datapath, statbuf.st_size)) {
177                 log_error("error sending firmware '%s' to device", firmware);
178                 set_loading(udev, loadpath, "-1");
179                 rc = EXIT_FAILURE;
180                 goto exit;
181         };
182
183         set_loading(udev, loadpath, "0");
184 exit:
185         if (fwfile)
186                 fclose(fwfile);
187         return rc;
188 }
189
190 const struct udev_builtin udev_builtin_firmware = {
191         .name = "firmware",
192         .cmd = builtin_firmware,
193         .help = "kernel firmware loader",
194         .run_once = true,
195 };