builtin.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /****************************************************************************
  2. * binfmt/builtin.c
  3. *
  4. * Copyright (C) 2012 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <sys/types.h>
  40. #include <sys/ioctl.h>
  41. #include <stdint.h>
  42. #include <string.h>
  43. #include <fcntl.h>
  44. #include <debug.h>
  45. #include <errno.h>
  46. #include <nuttx/fs/fs.h>
  47. #include <nuttx/fs/ioctl.h>
  48. #include <nuttx/binfmt/binfmt.h>
  49. #include <nuttx/binfmt/builtin.h>
  50. #ifdef CONFIG_BUILTIN
  51. /****************************************************************************
  52. * Pre-processor Definitions
  53. ****************************************************************************/
  54. /****************************************************************************
  55. * Private Function Prototypes
  56. ****************************************************************************/
  57. static int builtin_loadbinary(FAR struct binary_s *binp);
  58. /****************************************************************************
  59. * Private Data
  60. ****************************************************************************/
  61. static struct binfmt_s g_builtin_binfmt =
  62. {
  63. NULL, /* next */
  64. builtin_loadbinary, /* load */
  65. NULL, /* unload */
  66. };
  67. /****************************************************************************
  68. * Private Functions
  69. ****************************************************************************/
  70. /****************************************************************************
  71. * Name: builtin_loadbinary
  72. *
  73. * Description:
  74. * Verify that the file is an builtin binary.
  75. *
  76. ****************************************************************************/
  77. static int builtin_loadbinary(struct binary_s *binp)
  78. {
  79. FAR const char *filename;
  80. FAR const struct builtin_s *builtin;
  81. int fd;
  82. int index;
  83. int ret;
  84. binfo("Loading file: %s\n", binp->filename);
  85. /* Open the binary file for reading (only) */
  86. fd = nx_open(binp->filename, O_RDONLY);
  87. if (fd < 0)
  88. {
  89. berr("ERROR: Failed to open binary %s: %d\n", binp->filename, fd);
  90. return fd;
  91. }
  92. /* If this file is a BINFS file system, then we can recover the name of
  93. * the file using the FIOC_FILENAME ioctl() call.
  94. */
  95. ret = ioctl(fd, FIOC_FILENAME, (unsigned long)((uintptr_t)&filename));
  96. if (ret < 0)
  97. {
  98. int errval = get_errno();
  99. berr("ERROR: FIOC_FILENAME ioctl failed: %d\n", errval);
  100. close(fd);
  101. return -errval;
  102. }
  103. /* Other file systems may also support FIOC_FILENAME, so the real proof
  104. * is that we can look up the index to this name in g_builtins[].
  105. */
  106. index = builtin_isavail(filename);
  107. if (index < 0)
  108. {
  109. int errval = get_errno();
  110. berr("ERROR: %s is not a builtin application\n", filename);
  111. close(fd);
  112. return -errval;
  113. }
  114. /* Return the load information. NOTE: that there is no way to configure
  115. * the priority. That is a bug and needs to be fixed.
  116. */
  117. builtin = builtin_for_index(index);
  118. binp->entrypt = builtin->main;
  119. binp->stacksize = builtin->stacksize;
  120. binp->priority = builtin->priority;
  121. close(fd);
  122. return OK;
  123. }
  124. /****************************************************************************
  125. * Public Functions
  126. ****************************************************************************/
  127. /****************************************************************************
  128. * Name: builtin_initialize
  129. *
  130. * Description:
  131. * In order to use the builtin binary format, this function must be called
  132. * during system initialize to register the builtin binary format.
  133. *
  134. * Returned Value:
  135. * This is a NuttX internal function so it follows the convention that
  136. * 0 (OK) is returned on success and a negated errno is returned on
  137. * failure.
  138. *
  139. ****************************************************************************/
  140. int builtin_initialize(void)
  141. {
  142. int ret;
  143. /* Register ourselves as a binfmt loader */
  144. binfo("Registering Builtin Loader\n");
  145. ret = register_binfmt(&g_builtin_binfmt);
  146. if (ret != 0)
  147. {
  148. berr("Failed to register binfmt: %d\n", ret);
  149. }
  150. return ret;
  151. }
  152. /****************************************************************************
  153. * Name: builtin_uninitialize
  154. *
  155. * Description:
  156. * Unregister the builtin binary loader
  157. *
  158. * Returned Value:
  159. * None
  160. *
  161. ****************************************************************************/
  162. void builtin_uninitialize(void)
  163. {
  164. (void)unregister_binfmt(&g_builtin_binfmt);
  165. }
  166. #endif /* CONFIG_BUILTIN */