dev.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. /*
  2. * Copyright (c) 2019-2021, Arm Limited. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <cdefs.h>
  7. #include <common/debug.h>
  8. #include <lib/debugfs.h>
  9. #include <string.h>
  10. #include "dev.h"
  11. #define NR_MOUNT_POINTS 4
  12. struct mount_point {
  13. chan_t *new;
  14. chan_t *old;
  15. };
  16. /* This array contains all the available channels of the filesystem.
  17. * A file descriptor is the index of a specific channel in this array.
  18. */
  19. static chan_t fdset[NR_CHANS];
  20. /* This array contains all the available mount points of the filesystem. */
  21. static struct mount_point mount_points[NR_MOUNT_POINTS];
  22. /* This variable stores the channel associated to the root directory. */
  23. static chan_t slash_channel;
  24. /* This function creates a channel from a device index and registers
  25. * it to fdset.
  26. */
  27. static chan_t *create_new_channel(unsigned char index)
  28. {
  29. chan_t *channel = NULL;
  30. int i;
  31. for (i = 0; i < NR_CHANS; i++) {
  32. if (fdset[i].index == NODEV) {
  33. channel = &fdset[i];
  34. channel->index = index;
  35. break;
  36. }
  37. }
  38. return channel;
  39. }
  40. /*******************************************************************************
  41. * This function returns a pointer to an existing channel in fdset from a file
  42. * descriptor.
  43. ******************************************************************************/
  44. static chan_t *fd_to_channel(int fd)
  45. {
  46. if ((fd < 0) || (fd >= NR_CHANS) || (fdset[fd].index == NODEV)) {
  47. return NULL;
  48. }
  49. return &fdset[fd];
  50. }
  51. /*******************************************************************************
  52. * This function returns a file descriptor from a channel.
  53. * The caller must be sure that the channel is registered in fdset.
  54. ******************************************************************************/
  55. static int channel_to_fd(chan_t *channel)
  56. {
  57. return (channel == NULL) ? -1 : (channel - fdset);
  58. }
  59. /*******************************************************************************
  60. * This function checks the validity of a mode.
  61. ******************************************************************************/
  62. static bool is_valid_mode(int mode)
  63. {
  64. if ((mode & O_READ) && (mode & (O_WRITE | O_RDWR))) {
  65. return false;
  66. }
  67. if ((mode & O_WRITE) && (mode & (O_READ | O_RDWR))) {
  68. return false;
  69. }
  70. if ((mode & O_RDWR) && (mode & (O_READ | O_WRITE))) {
  71. return false;
  72. }
  73. return true;
  74. }
  75. /*******************************************************************************
  76. * This function extracts the next part of the given path contained and puts it
  77. * in token. It returns a pointer to the remainder of the path.
  78. ******************************************************************************/
  79. static const char *next(const char *path, char *token)
  80. {
  81. int index;
  82. const char *cursor;
  83. while (*path == '/') {
  84. ++path;
  85. }
  86. index = 0;
  87. cursor = path;
  88. if (*path != '\0') {
  89. while (*cursor != '/' && *cursor != '\0') {
  90. if (index == NAMELEN) {
  91. return NULL;
  92. }
  93. token[index++] = *cursor++;
  94. }
  95. }
  96. token[index] = '\0';
  97. return cursor;
  98. }
  99. /*******************************************************************************
  100. * This function returns the driver index in devtab of the driver
  101. * identified by id.
  102. ******************************************************************************/
  103. static int get_device_index(int id)
  104. {
  105. int index;
  106. dev_t * const *dp;
  107. for (index = 0, dp = devtab; *dp && (*dp)->id != id; ++dp) {
  108. index++;
  109. }
  110. if (*dp == NULL) {
  111. return -1;
  112. }
  113. return index;
  114. }
  115. /*******************************************************************************
  116. * This function clears a given channel fields
  117. ******************************************************************************/
  118. static void channel_clear(chan_t *channel)
  119. {
  120. channel->offset = 0;
  121. channel->qid = 0;
  122. channel->index = NODEV;
  123. channel->dev = 0;
  124. channel->mode = 0;
  125. }
  126. /*******************************************************************************
  127. * This function closes the channel pointed to by c.
  128. ******************************************************************************/
  129. void channel_close(chan_t *channel)
  130. {
  131. if (channel != NULL) {
  132. channel_clear(channel);
  133. }
  134. }
  135. /*******************************************************************************
  136. * This function copies data from src to dst after applying the offset of the
  137. * channel c. nbytes bytes are expected to be copied unless the data goes over
  138. * dst + len.
  139. * It returns the actual number of bytes that were copied.
  140. ******************************************************************************/
  141. int buf_to_channel(chan_t *channel, void *dst, void *src, int nbytes, long len)
  142. {
  143. const char *addr = src;
  144. if ((channel == NULL) || (dst == NULL) || (src == NULL)) {
  145. return 0;
  146. }
  147. if (channel->offset >= len) {
  148. return 0;
  149. }
  150. if ((channel->offset + nbytes) > len) {
  151. nbytes = len - channel->offset;
  152. }
  153. memcpy(dst, addr + channel->offset, nbytes);
  154. channel->offset += nbytes;
  155. return nbytes;
  156. }
  157. /*******************************************************************************
  158. * This function checks whether a channel (identified by its device index and
  159. * qid) is registered as a mount point.
  160. * Returns a pointer to the channel it is mounted to when found, NULL otherwise.
  161. ******************************************************************************/
  162. static chan_t *mount_point_to_channel(int index, qid_t qid)
  163. {
  164. chan_t *channel;
  165. struct mount_point *mp;
  166. for (mp = mount_points; mp < &mount_points[NR_MOUNT_POINTS]; mp++) {
  167. channel = mp->new;
  168. if (channel == NULL) {
  169. continue;
  170. }
  171. if ((channel->index == index) && (channel->qid == qid)) {
  172. return mp->old;
  173. }
  174. }
  175. return NULL;
  176. }
  177. /*******************************************************************************
  178. * This function calls the attach function of the driver identified by id.
  179. ******************************************************************************/
  180. chan_t *attach(int id, int dev)
  181. {
  182. /* Get the devtab index for the driver identified by id */
  183. int index = get_device_index(id);
  184. if (index < 0) {
  185. return NULL;
  186. }
  187. return devtab[index]->attach(id, dev);
  188. }
  189. /*******************************************************************************
  190. * This function is the default implementation of the driver attach function.
  191. * It creates a new channel and returns a pointer to it.
  192. ******************************************************************************/
  193. chan_t *devattach(int id, int dev)
  194. {
  195. chan_t *channel;
  196. int index;
  197. index = get_device_index(id);
  198. if (index < 0) {
  199. return NULL;
  200. }
  201. channel = create_new_channel(index);
  202. if (channel == NULL) {
  203. return NULL;
  204. }
  205. channel->dev = dev;
  206. channel->qid = CHDIR;
  207. return channel;
  208. }
  209. /*******************************************************************************
  210. * This function returns a channel given a path.
  211. * It goes through the filesystem, from the root namespace ('/') or from a
  212. * device namespace ('#'), switching channel on mount points.
  213. ******************************************************************************/
  214. chan_t *path_to_channel(const char *path, int mode)
  215. {
  216. int i, n;
  217. const char *path_next;
  218. chan_t *mnt, *channel;
  219. char elem[NAMELEN];
  220. if (path == NULL) {
  221. return NULL;
  222. }
  223. switch (path[0]) {
  224. case '/':
  225. channel = clone(&slash_channel, NULL);
  226. path_next = path;
  227. break;
  228. case '#':
  229. path_next = next(path + 1, elem);
  230. if (path_next == NULL) {
  231. goto noent;
  232. }
  233. n = 0;
  234. for (i = 1; (elem[i] >= '0') && (elem[i] <= '9'); i++) {
  235. n += elem[i] - '0';
  236. }
  237. if (elem[i] != '\0') {
  238. goto noent;
  239. }
  240. channel = attach(elem[0], n);
  241. break;
  242. default:
  243. return NULL;
  244. }
  245. if (channel == NULL) {
  246. return NULL;
  247. }
  248. for (path_next = next(path_next, elem); *elem;
  249. path_next = next(path_next, elem)) {
  250. if ((channel->qid & CHDIR) == 0) {
  251. goto notfound;
  252. }
  253. if (devtab[channel->index]->walk(channel, elem) < 0) {
  254. channel_close(channel);
  255. goto notfound;
  256. }
  257. mnt = mount_point_to_channel(channel->index, channel->qid);
  258. if (mnt != NULL) {
  259. clone(mnt, channel);
  260. }
  261. }
  262. if (path_next == NULL) {
  263. goto notfound;
  264. }
  265. /* TODO: check mode */
  266. return channel;
  267. notfound:
  268. channel_close(channel);
  269. noent:
  270. return NULL;
  271. }
  272. /*******************************************************************************
  273. * This function calls the clone function of the driver associated to the
  274. * channel c.
  275. ******************************************************************************/
  276. chan_t *clone(chan_t *c, chan_t *nc)
  277. {
  278. if (c->index == NODEV) {
  279. return NULL;
  280. }
  281. return devtab[c->index]->clone(c, nc);
  282. }
  283. /*******************************************************************************
  284. * This function is the default implementation of the driver clone function.
  285. * It creates a new channel and returns a pointer to it.
  286. * It clones channel into new_channel.
  287. ******************************************************************************/
  288. chan_t *devclone(chan_t *channel, chan_t *new_channel)
  289. {
  290. if (channel == NULL) {
  291. return NULL;
  292. }
  293. if (new_channel == NULL) {
  294. new_channel = create_new_channel(channel->index);
  295. if (new_channel == NULL) {
  296. return NULL;
  297. }
  298. }
  299. new_channel->qid = channel->qid;
  300. new_channel->dev = channel->dev;
  301. new_channel->mode = channel->mode;
  302. new_channel->offset = channel->offset;
  303. new_channel->index = channel->index;
  304. return new_channel;
  305. }
  306. /*******************************************************************************
  307. * This function is the default implementation of the driver walk function.
  308. * It goes through all the elements of tab using the gen function until a match
  309. * is found with name.
  310. * If a match is found, it copies the qid of the new directory.
  311. ******************************************************************************/
  312. int devwalk(chan_t *channel, const char *name, const dirtab_t *tab,
  313. int ntab, devgen_t *gen)
  314. {
  315. int i;
  316. dir_t dir;
  317. if ((channel == NULL) || (name == NULL) || (gen == NULL)) {
  318. return -1;
  319. }
  320. if ((name[0] == '.') && (name[1] == '\0')) {
  321. return 1;
  322. }
  323. for (i = 0; ; i++) {
  324. switch ((*gen)(channel, tab, ntab, i, &dir)) {
  325. case 0:
  326. /* Intentional fall-through */
  327. case -1:
  328. return -1;
  329. case 1:
  330. if (strncmp(name, dir.name, NAMELEN) != 0) {
  331. continue;
  332. }
  333. channel->qid = dir.qid;
  334. return 1;
  335. }
  336. }
  337. }
  338. /*******************************************************************************
  339. * This is a helper function which exposes the content of a directory, element
  340. * by element. It is meant to be called until the end of the directory is
  341. * reached or an error occurs.
  342. * It returns -1 on error, 0 on end of directory and 1 when a new file is found.
  343. ******************************************************************************/
  344. int dirread(chan_t *channel, dir_t *dir, const dirtab_t *tab,
  345. int ntab, devgen_t *gen)
  346. {
  347. int i, ret;
  348. if ((channel == NULL) || (dir == NULL) || (gen == NULL)) {
  349. return -1;
  350. }
  351. i = channel->offset/sizeof(dir_t);
  352. ret = (*gen)(channel, tab, ntab, i, dir);
  353. if (ret == 1) {
  354. channel->offset += sizeof(dir_t);
  355. }
  356. return ret;
  357. }
  358. /*******************************************************************************
  359. * This function sets the elements of dir.
  360. ******************************************************************************/
  361. void make_dir_entry(chan_t *channel, dir_t *dir,
  362. const char *name, long length, qid_t qid, unsigned int mode)
  363. {
  364. if ((channel == NULL) || (dir == NULL) || (name == NULL)) {
  365. return;
  366. }
  367. strlcpy(dir->name, name, sizeof(dir->name));
  368. dir->length = length;
  369. dir->qid = qid;
  370. dir->mode = mode;
  371. if ((qid & CHDIR) != 0) {
  372. dir->mode |= O_DIR;
  373. }
  374. dir->index = channel->index;
  375. dir->dev = channel->dev;
  376. }
  377. /*******************************************************************************
  378. * This function is the default implementation of the internal driver gen
  379. * function.
  380. * It copies and formats the information of the nth element of tab into dir.
  381. ******************************************************************************/
  382. int devgen(chan_t *channel, const dirtab_t *tab, int ntab, int n, dir_t *dir)
  383. {
  384. const dirtab_t *dp;
  385. if ((channel == NULL) || (dir == NULL) || (tab == NULL) ||
  386. (n >= ntab)) {
  387. return 0;
  388. }
  389. dp = &tab[n];
  390. make_dir_entry(channel, dir, dp->name, dp->length, dp->qid, dp->perm);
  391. return 1;
  392. }
  393. /*******************************************************************************
  394. * This function returns a file descriptor identifying the channel associated to
  395. * the given path.
  396. ******************************************************************************/
  397. int open(const char *path, int mode)
  398. {
  399. chan_t *channel;
  400. if (path == NULL) {
  401. return -1;
  402. }
  403. if (is_valid_mode(mode) == false) {
  404. return -1;
  405. }
  406. channel = path_to_channel(path, mode);
  407. return channel_to_fd(channel);
  408. }
  409. /*******************************************************************************
  410. * This function closes the channel identified by the file descriptor fd.
  411. ******************************************************************************/
  412. int close(int fd)
  413. {
  414. chan_t *channel;
  415. channel = fd_to_channel(fd);
  416. if (channel == NULL) {
  417. return -1;
  418. }
  419. channel_close(channel);
  420. return 0;
  421. }
  422. /*******************************************************************************
  423. * This function is the default implementation of the driver stat function.
  424. * It goes through all the elements of tab using the gen function until a match
  425. * is found with file.
  426. * If a match is found, dir contains the information file.
  427. ******************************************************************************/
  428. int devstat(chan_t *dirc, const char *file, dir_t *dir,
  429. const dirtab_t *tab, int ntab, devgen_t *gen)
  430. {
  431. int i, r = 0;
  432. chan_t *c, *mnt;
  433. if ((dirc == NULL) || (dir == NULL) || (gen == NULL)) {
  434. return -1;
  435. }
  436. c = path_to_channel(file, O_STAT);
  437. if (c == NULL) {
  438. return -1;
  439. }
  440. for (i = 0; ; i++) {
  441. switch ((*gen)(dirc, tab, ntab, i, dir)) {
  442. case 0:
  443. /* Intentional fall-through */
  444. case -1:
  445. r = -1;
  446. goto leave;
  447. case 1:
  448. mnt = mount_point_to_channel(dir->index, dir->qid);
  449. if (mnt != NULL) {
  450. dir->qid = mnt->qid;
  451. dir->index = mnt->index;
  452. }
  453. if ((dir->qid != c->qid) || (dir->index != c->index)) {
  454. continue;
  455. }
  456. goto leave;
  457. }
  458. }
  459. leave:
  460. channel_close(c);
  461. return r;
  462. }
  463. /*******************************************************************************
  464. * This function calls the stat function of the driver associated to the parent
  465. * directory of the file in path.
  466. * The result is stored in dir.
  467. ******************************************************************************/
  468. int stat(const char *path, dir_t *dir)
  469. {
  470. int r;
  471. size_t len;
  472. chan_t *channel;
  473. char *p, dirname[PATHLEN];
  474. if ((path == NULL) || (dir == NULL)) {
  475. return -1;
  476. }
  477. len = strlen(path);
  478. if ((len + 1) > sizeof(dirname)) {
  479. return -1;
  480. }
  481. memcpy(dirname, path, len);
  482. for (p = dirname + len; p > dirname; --p) {
  483. if (*p != '/') {
  484. break;
  485. }
  486. }
  487. p = memrchr(dirname, '/', p - dirname);
  488. if (p == NULL) {
  489. return -1;
  490. }
  491. dirname[p - dirname + 1] = '\0';
  492. channel = path_to_channel(dirname, O_STAT);
  493. if (channel == NULL) {
  494. return -1;
  495. }
  496. r = devtab[channel->index]->stat(channel, path, dir);
  497. channel_close(channel);
  498. return r;
  499. }
  500. /*******************************************************************************
  501. * This function calls the read function of the driver associated to fd.
  502. * It fills buf with at most n bytes.
  503. * It returns the number of bytes that were actually read.
  504. ******************************************************************************/
  505. int read(int fd, void *buf, int n)
  506. {
  507. chan_t *channel;
  508. if (buf == NULL) {
  509. return -1;
  510. }
  511. channel = fd_to_channel(fd);
  512. if (channel == NULL) {
  513. return -1;
  514. }
  515. if (((channel->qid & CHDIR) != 0) && (n < sizeof(dir_t))) {
  516. return -1;
  517. }
  518. return devtab[channel->index]->read(channel, buf, n);
  519. }
  520. /*******************************************************************************
  521. * This function calls the write function of the driver associated to fd.
  522. * It writes at most n bytes of buf.
  523. * It returns the number of bytes that were actually written.
  524. ******************************************************************************/
  525. int write(int fd, void *buf, int n)
  526. {
  527. chan_t *channel;
  528. if (buf == NULL) {
  529. return -1;
  530. }
  531. channel = fd_to_channel(fd);
  532. if (channel == NULL) {
  533. return -1;
  534. }
  535. if ((channel->qid & CHDIR) != 0) {
  536. return -1;
  537. }
  538. return devtab[channel->index]->write(channel, buf, n);
  539. }
  540. /*******************************************************************************
  541. * This function calls the seek function of the driver associated to fd.
  542. * It applies the offset off according to the strategy whence.
  543. ******************************************************************************/
  544. int seek(int fd, long off, int whence)
  545. {
  546. chan_t *channel;
  547. channel = fd_to_channel(fd);
  548. if (channel == NULL) {
  549. return -1;
  550. }
  551. if ((channel->qid & CHDIR) != 0) {
  552. return -1;
  553. }
  554. return devtab[channel->index]->seek(channel, off, whence);
  555. }
  556. /*******************************************************************************
  557. * This function is the default error implementation of the driver mount
  558. * function.
  559. ******************************************************************************/
  560. chan_t *deverrmount(chan_t *channel, const char *spec)
  561. {
  562. return NULL;
  563. }
  564. /*******************************************************************************
  565. * This function is the default error implementation of the driver write
  566. * function.
  567. ******************************************************************************/
  568. int deverrwrite(chan_t *channel, void *buf, int n)
  569. {
  570. return -1;
  571. }
  572. /*******************************************************************************
  573. * This function is the default error implementation of the driver seek
  574. * function.
  575. ******************************************************************************/
  576. int deverrseek(chan_t *channel, long off, int whence)
  577. {
  578. return -1;
  579. }
  580. /*******************************************************************************
  581. * This function is the default implementation of the driver seek function.
  582. * It applies the offset off according to the strategy whence to the channel c.
  583. ******************************************************************************/
  584. int devseek(chan_t *channel, long off, int whence)
  585. {
  586. switch (whence) {
  587. case KSEEK_SET:
  588. channel->offset = off;
  589. break;
  590. case KSEEK_CUR:
  591. channel->offset += off;
  592. break;
  593. case KSEEK_END:
  594. /* Not implemented */
  595. return -1;
  596. }
  597. return 0;
  598. }
  599. /*******************************************************************************
  600. * This function registers the channel associated to the path new as a mount
  601. * point for the channel c.
  602. ******************************************************************************/
  603. static int add_mount_point(chan_t *channel, const char *new)
  604. {
  605. int i;
  606. chan_t *cn;
  607. struct mount_point *mp;
  608. if (new == NULL) {
  609. goto err0;
  610. }
  611. cn = path_to_channel(new, O_READ);
  612. if (cn == NULL) {
  613. goto err0;
  614. }
  615. if ((cn->qid & CHDIR) == 0) {
  616. goto err1;
  617. }
  618. for (i = NR_MOUNT_POINTS - 1; i >= 0; i--) {
  619. mp = &mount_points[i];
  620. if (mp->new == NULL) {
  621. break;
  622. }
  623. }
  624. if (i < 0) {
  625. goto err1;
  626. }
  627. mp->new = cn;
  628. mp->old = channel;
  629. return 0;
  630. err1:
  631. channel_close(cn);
  632. err0:
  633. return -1;
  634. }
  635. /*******************************************************************************
  636. * This function registers the path new as a mount point for the path old.
  637. ******************************************************************************/
  638. int bind(const char *old, const char *new)
  639. {
  640. chan_t *channel;
  641. channel = path_to_channel(old, O_BIND);
  642. if (channel == NULL) {
  643. return -1;
  644. }
  645. if (add_mount_point(channel, new) < 0) {
  646. channel_close(channel);
  647. return -1;
  648. }
  649. return 0;
  650. }
  651. /*******************************************************************************
  652. * This function calls the mount function of the driver associated to the path
  653. * srv.
  654. * It mounts the path srv on the path where.
  655. ******************************************************************************/
  656. int mount(const char *srv, const char *where, const char *spec)
  657. {
  658. chan_t *channel, *mount_point_chan;
  659. int ret;
  660. channel = path_to_channel(srv, O_RDWR);
  661. if (channel == NULL) {
  662. goto err0;
  663. }
  664. mount_point_chan = devtab[channel->index]->mount(channel, spec);
  665. if (mount_point_chan == NULL) {
  666. goto err1;
  667. }
  668. ret = add_mount_point(mount_point_chan, where);
  669. if (ret < 0) {
  670. goto err2;
  671. }
  672. channel_close(channel);
  673. return 0;
  674. err2:
  675. channel_close(mount_point_chan);
  676. err1:
  677. channel_close(channel);
  678. err0:
  679. return -1;
  680. }
  681. /*******************************************************************************
  682. * This function initializes the device environment.
  683. * It creates the '/' channel.
  684. * It links the device drivers to the physical drivers.
  685. ******************************************************************************/
  686. void debugfs_init(void)
  687. {
  688. chan_t *channel, *cloned_channel;
  689. for (channel = fdset; channel < &fdset[NR_CHANS]; channel++) {
  690. channel_clear(channel);
  691. }
  692. channel = devattach('/', 0);
  693. if (channel == NULL) {
  694. panic();
  695. }
  696. cloned_channel = clone(channel, &slash_channel);
  697. if (cloned_channel == NULL) {
  698. panic();
  699. }
  700. channel_close(channel);
  701. devlink();
  702. }
  703. __dead2 void devpanic(const char *cause)
  704. {
  705. panic();
  706. }