devroot.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) 2019, Arm Limited. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <common/debug.h>
  8. #include <lib/debugfs.h>
  9. #include "blobs.h"
  10. #include "dev.h"
  11. /*******************************************************************************
  12. * This array contains the directories available from the root directory.
  13. ******************************************************************************/
  14. static const dirtab_t dirtab[] = {
  15. {"dev", CHDIR | DEV_ROOT_QDEV, 0, O_READ},
  16. {"blobs", CHDIR | DEV_ROOT_QBLOBS, 0, O_READ},
  17. {"fip", CHDIR | DEV_ROOT_QFIP, 0, O_READ}
  18. };
  19. static const dirtab_t devfstab[] = {
  20. };
  21. /*******************************************************************************
  22. * This function exposes the elements of the root directory.
  23. * It also exposes the content of the dev and blobs directories.
  24. ******************************************************************************/
  25. static int rootgen(chan_t *channel, const dirtab_t *tab, int ntab,
  26. int n, dir_t *dir)
  27. {
  28. switch (channel->qid & ~CHDIR) {
  29. case DEV_ROOT_QROOT:
  30. tab = dirtab;
  31. ntab = NELEM(dirtab);
  32. break;
  33. case DEV_ROOT_QDEV:
  34. tab = devfstab;
  35. ntab = NELEM(devfstab);
  36. break;
  37. case DEV_ROOT_QBLOBS:
  38. tab = blobtab;
  39. ntab = NELEM(blobtab);
  40. break;
  41. default:
  42. return 0;
  43. }
  44. return devgen(channel, tab, ntab, n, dir);
  45. }
  46. static int rootwalk(chan_t *channel, const char *name)
  47. {
  48. return devwalk(channel, name, NULL, 0, rootgen);
  49. }
  50. /*******************************************************************************
  51. * This function copies at most n bytes from the element referred by c into buf.
  52. ******************************************************************************/
  53. static int rootread(chan_t *channel, void *buf, int size)
  54. {
  55. const dirtab_t *dp;
  56. dir_t *dir;
  57. if ((channel->qid & CHDIR) != 0) {
  58. if (size < sizeof(dir_t)) {
  59. return -1;
  60. }
  61. dir = buf;
  62. return dirread(channel, dir, NULL, 0, rootgen);
  63. }
  64. /* Only makes sense when using debug language */
  65. assert(channel->qid != DEV_ROOT_QBLOBCTL);
  66. dp = &blobtab[channel->qid - DEV_ROOT_QBLOBCTL];
  67. return buf_to_channel(channel, buf, dp->data, size, dp->length);
  68. }
  69. static int rootstat(chan_t *channel, const char *file, dir_t *dir)
  70. {
  71. return devstat(channel, file, dir, NULL, 0, rootgen);
  72. }
  73. const dev_t rootdevtab = {
  74. .id = '/',
  75. .stat = rootstat,
  76. .clone = devclone,
  77. .attach = devattach,
  78. .walk = rootwalk,
  79. .read = rootread,
  80. .write = deverrwrite,
  81. .mount = deverrmount,
  82. .seek = devseek
  83. };