+static int import_program_into_env(struct udevice *udev, const char *program)
+{
+ char result[2048];
+ size_t reslen;
+
+ if (run_program(program, udev->dev->subsystem, result, sizeof(result), &reslen) != 0)
+ return -1;
+ return import_keys_into_env(udev, result, reslen);
+}
+
+static int import_parent_into_env(struct udevice *udev, const char *filter)
+{
+ struct sysfs_device *dev_parent;
+ int rc = -1;
+
+ dev_parent = sysfs_device_get_parent(udev->dev);
+ if (dev_parent != NULL) {
+ struct udevice *udev_parent;
+ struct name_entry *name_loop;
+
+ dbg("found parent '%s', get the node name", dev_parent->devpath);
+ udev_parent = udev_device_init(NULL);
+ if (udev_parent == NULL)
+ return -1;
+ /* import the udev_db of the parent */
+ if (udev_db_get_device(udev_parent, dev_parent->devpath) == 0) {
+ dbg("import stored parent env '%s'", udev_parent->name);
+ list_for_each_entry(name_loop, &udev_parent->env_list, node) {
+ char name[NAME_SIZE];
+ char *pos;
+
+ strlcpy(name, name_loop->name, sizeof(name));
+ pos = strchr(name, '=');
+ if (pos) {
+ pos[0] = '\0';
+ pos++;
+ if (fnmatch(filter, name, 0) == 0) {
+ dbg("import key '%s'", name_loop->name);
+ name_list_add(&udev->env_list, name_loop->name, 0);
+ setenv(name, pos, 1);
+ } else
+ dbg("skip key '%s'", name_loop->name);
+ }
+ }
+ rc = 0;
+ } else
+ dbg("parent not found in database");
+ udev_device_cleanup(udev_parent);
+ }
+
+ return rc;
+}
+
+static int pass_env_to_socket(const char *sockname, const char *devpath, const char *action)
+{
+ int sock;
+ struct sockaddr_un saddr;
+ socklen_t addrlen;
+ char buf[2048];
+ size_t bufpos = 0;
+ int i;
+ ssize_t count;
+ int retval = 0;
+
+ dbg("pass environment to socket '%s'", sockname);
+ sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
+ memset(&saddr, 0x00, sizeof(struct sockaddr_un));
+ saddr.sun_family = AF_LOCAL;
+ /* abstract namespace only */
+ strcpy(&saddr.sun_path[1], sockname);
+ addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
+
+ bufpos = snprintf(buf, sizeof(buf)-1, "%s@%s", action, devpath);
+ bufpos++;
+ for (i = 0; environ[i] != NULL && bufpos < sizeof(buf); i++) {
+ bufpos += strlcpy(&buf[bufpos], environ[i], sizeof(buf) - bufpos-1);
+ bufpos++;
+ }
+
+ count = sendto(sock, &buf, bufpos, 0, (struct sockaddr *)&saddr, addrlen);
+ if (count < 0)
+ retval = -1;
+ info("passed %zi bytes to socket '%s', ", count, sockname);
+
+ close(sock);
+ return retval;
+}
+
+int udev_rules_run(struct udevice *udev)
+{
+ struct name_entry *name_loop;
+ int retval = 0;
+
+ dbg("executing run list");
+ list_for_each_entry(name_loop, &udev->run_list, node) {
+ if (strncmp(name_loop->name, "socket:", strlen("socket:")) == 0) {
+ pass_env_to_socket(&name_loop->name[strlen("socket:")], udev->dev->devpath, udev->action);
+ } else {
+ char program[PATH_SIZE];
+
+ strlcpy(program, name_loop->name, sizeof(program));
+ udev_rules_apply_format(udev, program, sizeof(program));
+ if (run_program(program, udev->dev->subsystem, NULL, 0, NULL) != 0)
+ if (!name_loop->ignore_error)
+ retval = -1;
+ }
+ }
+
+ return retval;
+}
+
+#define WAIT_LOOP_PER_SECOND 50
+static int wait_for_sysfs(struct udevice *udev, const char *file, int timeout)
+{
+ char devicepath[PATH_SIZE];
+ char filepath[PATH_SIZE];
+ struct stat stats;
+ int loop = timeout * WAIT_LOOP_PER_SECOND;
+
+ strlcpy(devicepath, sysfs_path, sizeof(devicepath));
+ strlcat(devicepath, udev->dev->devpath, sizeof(devicepath));
+ strlcpy(filepath, devicepath, sizeof(filepath));
+ strlcat(filepath, "/", sizeof(filepath));
+ strlcat(filepath, file, sizeof(filepath));
+
+ dbg("will wait %i sec for '%s'", timeout, filepath);
+ while (--loop) {
+ /* lookup file */
+ if (stat(filepath, &stats) == 0) {
+ info("file '%s' appeared after %i loops", filepath, (timeout * WAIT_LOOP_PER_SECOND) - loop-1);
+ return 0;
+ }
+ /* make sure, the device did not disappear in the meantime */
+ if (stat(devicepath, &stats) != 0) {
+ info("device disappeared while waiting for '%s'", filepath);
+ return -2;
+ }
+ info("wait for '%s' for %i mseconds", filepath, 1000 / WAIT_LOOP_PER_SECOND);
+ usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
+ }
+ info("waiting for '%s' failed", filepath);
+ return -1;
+}
+
+/* handle "[$SUBSYSTEM/$KERNEL]<attribute>" lookup */
+static int attr_get_by_subsys_id(const char *attrstr, char *devpath, size_t len, char **attr)
+{
+ char subsys[NAME_SIZE];
+ char *attrib;
+ char *id;
+ int found = 0;
+
+ if (attrstr[0] != '[')
+ goto out;
+
+ strlcpy(subsys, &attrstr[1], sizeof(subsys));
+
+ attrib = strchr(subsys, ']');
+ if (attrib == NULL)
+ goto out;
+ attrib[0] = '\0';
+ attrib = &attrib[1];
+
+ id = strchr(subsys, '/');
+ if (id == NULL)
+ goto out;
+ id[0] = '\0';
+ id = &id[1];
+
+ if (sysfs_lookup_devpath_by_subsys_id(devpath, len, subsys, id)) {
+ if (attr != NULL) {
+ if (attrib[0] != '\0')
+ *attr = attrib;
+ else
+ *attr = NULL;
+ }
+ found = 1;
+ }
+out:
+ return found;
+}
+
+void udev_rules_apply_format(struct udevice *udev, char *string, size_t maxsize)