modlib_init.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /****************************************************************************
  2. * libs/libc/modlib/modlib_init.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/stat.h>
  25. #include <stdint.h>
  26. #include <string.h>
  27. #include <fcntl.h>
  28. #include <debug.h>
  29. #include <errno.h>
  30. #include <nuttx/fs/fs.h>
  31. #include <nuttx/lib/modlib.h>
  32. #include "modlib/modlib.h"
  33. /****************************************************************************
  34. * Pre-processor Definitions
  35. ****************************************************************************/
  36. /* CONFIG_DEBUG_FEATURES, CONFIG_DEBUG_INFO, and CONFIG_MODLIB_DUMPBUFFER
  37. * have to be defined or CONFIG_MODLIB_DUMPBUFFER does nothing.
  38. */
  39. #if !defined(CONFIG_DEBUG_INFO) || !defined (CONFIG_MODLIB_DUMPBUFFER)
  40. # undef CONFIG_MODLIB_DUMPBUFFER
  41. #endif
  42. #ifdef CONFIG_MODLIB_DUMPBUFFER
  43. # define modlib_dumpbuffer(m,b,n) binfodumpbuffer(m,b,n)
  44. #else
  45. # define modlib_dumpbuffer(m,b,n)
  46. #endif
  47. /****************************************************************************
  48. * Private Functions
  49. ****************************************************************************/
  50. /****************************************************************************
  51. * Name: modlib_filelen
  52. *
  53. * Description:
  54. * Get the size of the ELF file
  55. *
  56. * Returned Value:
  57. * 0 (OK) is returned on success and a negated errno is returned on
  58. * failure.
  59. *
  60. ****************************************************************************/
  61. static inline int modlib_filelen(FAR struct mod_loadinfo_s *loadinfo,
  62. FAR const char *filename)
  63. {
  64. struct stat buf;
  65. int ret;
  66. /* Get the file stats */
  67. ret = stat(filename, &buf);
  68. if (ret < 0)
  69. {
  70. int errval = get_errno();
  71. berr("ERROR: Failed to stat file: %d\n", errval);
  72. return -errval;
  73. }
  74. /* Verify that it is a regular file */
  75. if (!S_ISREG(buf.st_mode))
  76. {
  77. berr("ERROR: Not a regular file. mode: %d\n", buf.st_mode);
  78. return -ENOENT;
  79. }
  80. /* TODO: Verify that the file is readable. Not really important because
  81. * we will detect this when we try to open the file read-only.
  82. */
  83. /* Return the size of the file in the loadinfo structure */
  84. loadinfo->filelen = buf.st_size;
  85. return OK;
  86. }
  87. /****************************************************************************
  88. * Public Functions
  89. ****************************************************************************/
  90. /****************************************************************************
  91. * Name: modlib_initialize
  92. *
  93. * Description:
  94. * This function is called to configure the library to process an ELF
  95. * program binary.
  96. *
  97. * Returned Value:
  98. * 0 (OK) is returned on success and a negated errno is returned on
  99. * failure.
  100. *
  101. ****************************************************************************/
  102. int modlib_initialize(FAR const char *filename,
  103. FAR struct mod_loadinfo_s *loadinfo)
  104. {
  105. int ret;
  106. binfo("filename: %s loadinfo: %p\n", filename, loadinfo);
  107. /* Clear the load info structure */
  108. memset(loadinfo, 0, sizeof(struct mod_loadinfo_s));
  109. /* Get the length of the file. */
  110. ret = modlib_filelen(loadinfo, filename);
  111. if (ret < 0)
  112. {
  113. berr("ERROR: modlib_filelen failed: %d\n", ret);
  114. return ret;
  115. }
  116. /* Open the binary file for reading (only) */
  117. loadinfo->filfd = _NX_OPEN(filename, O_RDONLY);
  118. if (loadinfo->filfd < 0)
  119. {
  120. int errval = _NX_GETERRNO(loadinfo->filfd);
  121. berr("ERROR: Failed to open ELF binary %s: %d\n", filename, errval);
  122. return -errval;
  123. }
  124. /* Read the ELF ehdr from offset 0 */
  125. ret = modlib_read(loadinfo, (FAR uint8_t *)&loadinfo->ehdr,
  126. sizeof(Elf_Ehdr), 0);
  127. if (ret < 0)
  128. {
  129. berr("ERROR: Failed to read ELF header: %d\n", ret);
  130. return ret;
  131. }
  132. modlib_dumpbuffer("ELF header", (FAR const uint8_t *)&loadinfo->ehdr,
  133. sizeof(Elf_Ehdr));
  134. /* Verify the ELF header */
  135. ret = modlib_verifyheader(&loadinfo->ehdr);
  136. if (ret < 0)
  137. {
  138. /* This may not be an error because we will be called to attempt
  139. * loading EVERY binary. If modlib_verifyheader() does not recognize
  140. * the ELF header, it will -ENOEXEC which simply informs the system
  141. * that the file is not an ELF file. modlib_verifyheader() will return
  142. * other errors if the ELF header is not correctly formed.
  143. */
  144. berr("ERROR: Bad ELF header: %d\n", ret);
  145. return ret;
  146. }
  147. return OK;
  148. }