os.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Operating System Interface
  3. *
  4. * This provides access to useful OS routines for the sandbox architecture.
  5. * They are kept in a separate file so we can include system headers.
  6. *
  7. * Copyright (c) 2011 The Chromium OS Authors.
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. #ifndef __OS_H__
  27. #define __OS_H__
  28. struct sandbox_state;
  29. /**
  30. * Access to the OS read() system call
  31. *
  32. * \param fd File descriptor as returned by os_open()
  33. * \param buf Buffer to place data
  34. * \param count Number of bytes to read
  35. * \return number of bytes read, or -1 on error
  36. */
  37. ssize_t os_read(int fd, void *buf, size_t count);
  38. /**
  39. * Access to the OS read() system call with non-blocking access
  40. *
  41. * \param fd File descriptor as returned by os_open()
  42. * \param buf Buffer to place data
  43. * \param count Number of bytes to read
  44. * \return number of bytes read, or -1 on error
  45. */
  46. ssize_t os_read_no_block(int fd, void *buf, size_t count);
  47. /**
  48. * Access to the OS write() system call
  49. *
  50. * \param fd File descriptor as returned by os_open()
  51. * \param buf Buffer containing data to write
  52. * \param count Number of bytes to write
  53. * \return number of bytes written, or -1 on error
  54. */
  55. ssize_t os_write(int fd, const void *buf, size_t count);
  56. /**
  57. * Access to the OS lseek() system call
  58. *
  59. * \param fd File descriptor as returned by os_open()
  60. * \param offset File offset (based on whence)
  61. * \param whence Position offset is relative to (see below)
  62. * \return new file offset
  63. */
  64. off_t os_lseek(int fd, off_t offset, int whence);
  65. /* Defines for "whence" in os_lseek() */
  66. #define OS_SEEK_SET 0
  67. #define OS_SEEK_CUR 1
  68. #define OS_SEEK_END 2
  69. /**
  70. * Access to the OS open() system call
  71. *
  72. * \param pathname Pathname of file to open
  73. * \param flags Flags, like O_RDONLY, O_RDWR
  74. * \return file descriptor, or -1 on error
  75. */
  76. int os_open(const char *pathname, int flags);
  77. #define OS_O_RDONLY 0
  78. #define OS_O_WRONLY 1
  79. #define OS_O_RDWR 2
  80. #define OS_O_MASK 3 /* Mask for read/write flags */
  81. #define OS_O_CREAT 0100
  82. /**
  83. * Access to the OS close() system call
  84. *
  85. * \param fd File descriptor to close
  86. * \return 0 on success, -1 on error
  87. */
  88. int os_close(int fd);
  89. /**
  90. * Access to the OS exit() system call
  91. *
  92. * This exits with the supplied return code, which should be 0 to indicate
  93. * success.
  94. *
  95. * @param exit_code exit code for U-Boot
  96. */
  97. void os_exit(int exit_code) __attribute__((noreturn));
  98. /**
  99. * Put tty into raw mode to mimic serial console better
  100. */
  101. void os_tty_raw(int fd);
  102. /**
  103. * Acquires some memory from the underlying os.
  104. *
  105. * \param length Number of bytes to be allocated
  106. * \return Pointer to length bytes or NULL on error
  107. */
  108. void *os_malloc(size_t length);
  109. /**
  110. * Access to the usleep function of the os
  111. *
  112. * \param usec Time to sleep in micro seconds
  113. */
  114. void os_usleep(unsigned long usec);
  115. /**
  116. * Gets a monotonic increasing number of nano seconds from the OS
  117. *
  118. * \return A monotonic increasing time scaled in nano seconds
  119. */
  120. u64 os_get_nsec(void);
  121. /**
  122. * Parse arguments and update sandbox state.
  123. *
  124. * @param state Sandbox state to update
  125. * @param argc Argument count
  126. * @param argv Argument vector
  127. * @return 0 if ok, and program should continue;
  128. * 1 if ok, but program should stop;
  129. * -1 on error: program should terminate.
  130. */
  131. int os_parse_args(struct sandbox_state *state, int argc, char *argv[]);
  132. /*
  133. * Types of directory entry that we support. See also os_dirent_typename in
  134. * the C file.
  135. */
  136. enum os_dirent_t {
  137. OS_FILET_REG, /* Regular file */
  138. OS_FILET_LNK, /* Symbolic link */
  139. OS_FILET_DIR, /* Directory */
  140. OS_FILET_UNKNOWN, /* Something else */
  141. OS_FILET_COUNT,
  142. };
  143. /** A directory entry node, containing information about a single dirent */
  144. struct os_dirent_node {
  145. struct os_dirent_node *next; /* Pointer to next node, or NULL */
  146. ulong size; /* Size of file in bytes */
  147. enum os_dirent_t type; /* Type of entry */
  148. char name[0]; /* Name of entry */
  149. };
  150. /**
  151. * Get a directionry listing
  152. *
  153. * This allocates and returns a linked list containing the directory listing.
  154. *
  155. * @param dirname Directory to examine
  156. * @param headp Returns pointer to head of linked list, or NULL if none
  157. * @return 0 if ok, -ve on error
  158. */
  159. int os_dirent_ls(const char *dirname, struct os_dirent_node **headp);
  160. /**
  161. * Get the name of a directory entry type
  162. *
  163. * @param type Type to cehck
  164. * @return string containing the name of that type, or "???" if none/invalid
  165. */
  166. const char *os_dirent_get_typename(enum os_dirent_t type);
  167. /**
  168. * Get the size of a file
  169. *
  170. * @param fname Filename to check
  171. * @return size of file, or -1 if an error ocurred
  172. */
  173. ssize_t os_get_filesize(const char *fname);
  174. #endif