builtin.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /****************************************************************************
  2. * binfmt/builtin.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. #include <sys/types.h>
  25. #include <sys/ioctl.h>
  26. #include <stdint.h>
  27. #include <string.h>
  28. #include <fcntl.h>
  29. #include <debug.h>
  30. #include <errno.h>
  31. #include <nuttx/fs/fs.h>
  32. #include <nuttx/fs/ioctl.h>
  33. #include <nuttx/binfmt/binfmt.h>
  34. #include <nuttx/lib/builtin.h>
  35. #ifdef CONFIG_FS_BINFS
  36. /****************************************************************************
  37. * Private Function Prototypes
  38. ****************************************************************************/
  39. static int builtin_loadbinary(FAR struct binary_s *binp,
  40. FAR const char *filename,
  41. FAR const struct symtab_s *exports,
  42. int nexports);
  43. /****************************************************************************
  44. * Private Data
  45. ****************************************************************************/
  46. static struct binfmt_s g_builtin_binfmt =
  47. {
  48. NULL, /* next */
  49. builtin_loadbinary, /* load */
  50. NULL, /* unload */
  51. };
  52. /****************************************************************************
  53. * Private Functions
  54. ****************************************************************************/
  55. /****************************************************************************
  56. * Name: builtin_loadbinary
  57. *
  58. * Description:
  59. * Verify that the file is an builtin binary.
  60. *
  61. ****************************************************************************/
  62. static int builtin_loadbinary(FAR struct binary_s *binp,
  63. FAR const char *filename,
  64. FAR const struct symtab_s *exports,
  65. int nexports)
  66. {
  67. FAR const struct builtin_s *builtin;
  68. struct file file;
  69. int index;
  70. int ret;
  71. binfo("Loading file: %s\n", filename);
  72. /* Open the binary file for reading (only) */
  73. ret = file_open(&file, filename, O_RDONLY);
  74. if (ret < 0)
  75. {
  76. berr("ERROR: Failed to open binary %s: %d\n", filename, ret);
  77. return ret;
  78. }
  79. /* If this file is a BINFS file system, then we can recover the name of
  80. * the file using the FIOC_FILENAME ioctl() call.
  81. */
  82. ret = file_ioctl(&file, FIOC_FILENAME,
  83. (unsigned long)((uintptr_t)&filename));
  84. if (ret < 0)
  85. {
  86. berr("ERROR: FIOC_FILENAME ioctl failed: %d\n", ret);
  87. file_close(&file);
  88. return ret;
  89. }
  90. /* Other file systems may also support FIOC_FILENAME, so the real proof
  91. * is that we can look up the index to this name in g_builtins[].
  92. */
  93. index = builtin_isavail(filename);
  94. if (index < 0)
  95. {
  96. berr("ERROR: %s is not a builtin application\n", filename);
  97. file_close(&file);
  98. return index;
  99. }
  100. /* Return the load information. NOTE: that there is no way to configure
  101. * the priority. That is a bug and needs to be fixed.
  102. */
  103. builtin = builtin_for_index(index);
  104. binp->entrypt = builtin->main;
  105. binp->stacksize = builtin->stacksize;
  106. binp->priority = builtin->priority;
  107. file_close(&file);
  108. return OK;
  109. }
  110. /****************************************************************************
  111. * Public Functions
  112. ****************************************************************************/
  113. /****************************************************************************
  114. * Name: builtin_initialize
  115. *
  116. * Description:
  117. * In order to use the builtin binary format, this function must be called
  118. * during system initialize to register the builtin binary format.
  119. *
  120. * Returned Value:
  121. * This is a NuttX internal function so it follows the convention that
  122. * 0 (OK) is returned on success and a negated errno is returned on
  123. * failure.
  124. *
  125. ****************************************************************************/
  126. int builtin_initialize(void)
  127. {
  128. int ret;
  129. /* Register ourselves as a binfmt loader */
  130. binfo("Registering Builtin Loader\n");
  131. ret = register_binfmt(&g_builtin_binfmt);
  132. if (ret != 0)
  133. {
  134. berr("Failed to register binfmt: %d\n", ret);
  135. }
  136. return ret;
  137. }
  138. /****************************************************************************
  139. * Name: builtin_uninitialize
  140. *
  141. * Description:
  142. * Unregister the builtin binary loader
  143. *
  144. * Returned Value:
  145. * None
  146. *
  147. ****************************************************************************/
  148. void builtin_uninitialize(void)
  149. {
  150. unregister_binfmt(&g_builtin_binfmt);
  151. }
  152. #endif /* CONFIG_FS_BINFS */