1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
|
/***************************************************************************
* (C) Copyright 2006, 2007 Alastair Poole. <alastairpoole@gmail.com> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
#include "of_api.h"
#include <errno.h>
struct device_node *_of_return_nodes(struct device_node **array, int *idx,
int *sem, int type)
{
while (*idx >= 0) {
if (*idx == 0 || type == 0)
*sem = 0;
return *idx > 0 ? array[(*idx)--] : NULL;
}
return NULL;
}
char *_of_read_name(const char *path, struct device_node *node)
{
int fd;
char *tmp;
char buf[PATH_MAX];
struct stat fstats;
uint32_t size;
snprintf(buf, sizeof(buf), "%s%s", path, "name");
if (stat(buf, &fstats) < 0)
return NULL;
size = fstats.st_size;
if ((fd = open(buf, O_RDONLY)) < 0)
return NULL;
if (node) {
node->name = calloc(1, size);
if(!node->name)
of_error("calloc()");
tmp = node->name;
} else {
tmp = calloc(1, size);
if(!tmp)
of_error("calloc()");
}
if (read(fd, tmp, size) != size)
of_error("read()");
close(fd);
return tmp;
}
void _of_read_type(const char *path, struct device_node *node)
{
int fd;
char buf[PATH_MAX];
struct stat fstats;
uint32_t size;
snprintf(buf, sizeof(buf),"%s%s", path, "device_type");
if (stat(buf, &fstats) < 0)
return;
size = fstats.st_size;
if ((fd = open(buf, O_RDONLY)) < 0)
return;
node->type = calloc(1, size);
if(!node->type)
of_error("calloc()");
if (read(fd, node->type, size) != size)
of_error("read()");
close(fd);
}
void _of_read_linux_phandle(const char *path, struct device_node *node)
{
int fd;
char buf[PATH_MAX];
struct stat fstats;
uint32_t size;
snprintf(buf, sizeof(buf), "%s%s", path, "linux,phandle");
if (stat(buf, &fstats) < 0)
return;
size = fstats.st_size;
if(!size)
return;
if ((fd = open(buf, O_RDONLY)) < 0)
return;
node->linux_phandle.len = size;
node->linux_phandle.data = calloc(1, size);
if (read(fd, node->linux_phandle.data, size) != size)
of_error("read()");
close(fd);
}
void _of_get_path(char *path)
{
char *ptr;
char *p;
int i;
i = strlen(OF_ROOT);
ptr = strdup(path);
p = &ptr[i];
sprintf(path, "%s", p);
free(ptr);
}
struct device_node *_of_populate_node(const char *path, const char *name)
{
struct device_node *tmp = calloc(1, sizeof(struct device_node));
char *p = strdup(path);
_of_remove_filename(p);
tmp->full_path = strdup(p);
if (name)
tmp->name = strdup(name);
else
_of_read_name(path, tmp);
_of_get_path(p);
tmp->path = strdup(p);
free(p);
_of_read_linux_phandle(tmp->full_path, tmp);
_of_read_type(tmp->full_path, tmp);
return tmp;
}
struct device_node *_of_get_type(const char *path, const char *type)
{
int fd;
char buf[PATH_MAX];
char *name;
char *ptr;
struct device_node *tmp = NULL;
struct stat fstats;
uint32_t size;
if (stat(path, &fstats) < 0) {
exit(EXIT_FAILURE);
}
size = fstats.st_size;
if(!size)
return NULL;
if ((fd = open(path, O_RDONLY)) < 0)
of_error("open()");
if (read(fd, buf, size) != size)
of_error("read()");
if (memcmp(buf, type, size))
goto out;
ptr = strdup(path);
_of_remove_filename(ptr);
name = _of_read_name(ptr, NULL);
tmp = _of_populate_node(path, name);
free(ptr);
free(name);
out:
close(fd);
return tmp;
}
void _of_remove_filename(char *path)
{
char *ptr;
ptr = strrchr(path, '/');
*(ptr + 1) = '\0';
}
uint32_t _of_phandle_to_int(struct node_property_t phandle)
{
uint32_t tmp = 0;
if (phandle.len == 4)
tmp =
(phandle.data[0] << 24) + (phandle.data[1] << 16) +
(phandle.data[2] << 8) + phandle.data[3];
return tmp;
}
void _of_make_compat_path(const char *path, char *buf)
{
size_t slen = strlen(path);
int changed = 0;
if (*path != '/')
changed = 1;
if(!strlen(path)) {
snprintf(buf, PATH_MAX, "%s/", OF_ROOT);
return;
}
if (path[slen - 1] != '/') {
if (changed)
snprintf(buf, PATH_MAX, "%s/%s/", OF_ROOT, path);
else
snprintf(buf, PATH_MAX, "%s%s/", OF_ROOT, path);
} else
snprintf(buf, PATH_MAX, "%s%s", OF_ROOT, path);
}
struct device_node *_of_get_phandle(const char *path, const uint32_t * phandle)
{
struct node_property_t props;
struct device_node *tmp = NULL;
uint32_t size, val = 0;
struct stat fstats;
int fd;
char *ptr;
char *name;
if (stat(path, &fstats) < 0)
return NULL;
size = fstats.st_size;
if(!size)
return NULL;
props.data = malloc(size);
if(!props.data)
of_error("calloc()");
props.len = size;
if ((fd = open(path, O_RDONLY)) < 0)
of_error("open()");
if (read(fd, props.data, size) != size)
of_error("read()");
close(fd);
val = _of_phandle_to_int(props);
free(props.data);
if (val == *phandle) {
ptr = strdup(path);
_of_remove_filename(ptr);
name = _of_read_name(ptr, NULL);
tmp = _of_populate_node(path, name);
free(ptr);
free(name);
}
return tmp;
}
struct device_node *_of_get_name(const char *path, const char *name)
{
int fd;
uint32_t size;
char buf[PATH_MAX];
struct device_node *tmp = NULL;
struct stat fstats;
if (stat(path, &fstats) < 0)
return NULL;
size = fstats.st_size;
if(!size)
return NULL;
if ((fd = open(path, O_RDONLY)) < 0)
of_error("open()");
if (read(fd, buf, size) != size)
of_error("read()");
if (memcmp(buf, name, size))
goto out;
tmp = _of_populate_node(path, name);
out:
close(fd);
return tmp;
}
void _of_find_node_by_parse(char *path, const void *search, uint16_t type, int full)
{
DIR *dir;
struct dirent *tmp = NULL;
char *directories[8192] = { NULL };
char fullpath[PATH_MAX];
int x = 0;
struct stat fstats;
struct device_node *node = NULL;
lstat(path, &fstats);
if (S_ISLNK(fstats.st_mode))
return;
if ((dir = opendir(path)) == NULL)
return;
while ((tmp = readdir(dir)) != NULL) {
if ((strcmp(tmp->d_name, ".")) && (strcmp(tmp->d_name, ".."))) {
if (!strcmp(path, "/"))
strcat(strcpy(fullpath, "/"), tmp->d_name);
else
strcat(strcat(strcpy(fullpath, path), "/"),
tmp->d_name);
if (type == OF_SEARCH_NAME) {
if (!strcmp(tmp->d_name, "name")) {
if ((node =
_of_get_name(fullpath,
search)) != NULL) {
_n_array[++_n_idx] = node;
_n_sem = 1;
if(full==0)
goto out;
}
}
}
if (type == OF_SEARCH_TYPE) {
if (!strcmp(tmp->d_name, "device_type")) {
if ((node =
_of_get_type(fullpath,
search)) != NULL) {
_t_array[++_t_idx] = node;
_t_sem = 1;
if(full==0)
goto out;
}
}
}
if (type == OF_SEARCH_PHDL) {
if (!strcmp(tmp->d_name, "linux,phandle")) {
if ((node =
_of_get_phandle(fullpath,
search)) != NULL) {
_p_array[++_p_idx] = node;
_p_sem = 1;
goto out;
}
}
}
lstat(fullpath, &fstats);
if (S_ISDIR(fstats.st_mode))
directories[x++] = strdup(fullpath);
}
}
x = 0;
while (directories[x] != NULL) {
_of_find_node_by_parse(directories[x], search, type, full);
out:
free(directories[x++]);
}
closedir(dir);
}
|