17a9219c1SSimon Glass /* 2d9165153SSimon Glass * Operating System Interface 3d9165153SSimon Glass * 4d9165153SSimon Glass * This provides access to useful OS routines for the sandbox architecture. 5d9165153SSimon Glass * They are kept in a separate file so we can include system headers. 6d9165153SSimon Glass * 77a9219c1SSimon Glass * Copyright (c) 2011 The Chromium OS Authors. 81a459660SWolfgang Denk * SPDX-License-Identifier: GPL-2.0+ 97a9219c1SSimon Glass */ 107a9219c1SSimon Glass 114f345d56SMike Frysinger #ifndef __OS_H__ 124f345d56SMike Frysinger #define __OS_H__ 134f345d56SMike Frysinger 142a54d159SSimon Glass #include <linux/types.h> 152a54d159SSimon Glass 1694eefdeeSSimon Glass struct rtc_time; 1770db4212SSimon Glass struct sandbox_state; 1870db4212SSimon Glass 197a9219c1SSimon Glass /** 207a9219c1SSimon Glass * Access to the OS read() system call 217a9219c1SSimon Glass * 227a9219c1SSimon Glass * \param fd File descriptor as returned by os_open() 237a9219c1SSimon Glass * \param buf Buffer to place data 247a9219c1SSimon Glass * \param count Number of bytes to read 257a9219c1SSimon Glass * \return number of bytes read, or -1 on error 267a9219c1SSimon Glass */ 277a9219c1SSimon Glass ssize_t os_read(int fd, void *buf, size_t count); 287a9219c1SSimon Glass 297a9219c1SSimon Glass /** 30e101550aSTaylor Hutt * Access to the OS read() system call with non-blocking access 31e101550aSTaylor Hutt * 32e101550aSTaylor Hutt * \param fd File descriptor as returned by os_open() 33e101550aSTaylor Hutt * \param buf Buffer to place data 34e101550aSTaylor Hutt * \param count Number of bytes to read 35e101550aSTaylor Hutt * \return number of bytes read, or -1 on error 36e101550aSTaylor Hutt */ 37e101550aSTaylor Hutt ssize_t os_read_no_block(int fd, void *buf, size_t count); 38e101550aSTaylor Hutt 39e101550aSTaylor Hutt /** 407a9219c1SSimon Glass * Access to the OS write() system call 417a9219c1SSimon Glass * 427a9219c1SSimon Glass * \param fd File descriptor as returned by os_open() 437a9219c1SSimon Glass * \param buf Buffer containing data to write 447a9219c1SSimon Glass * \param count Number of bytes to write 457a9219c1SSimon Glass * \return number of bytes written, or -1 on error 467a9219c1SSimon Glass */ 477a9219c1SSimon Glass ssize_t os_write(int fd, const void *buf, size_t count); 487a9219c1SSimon Glass 497a9219c1SSimon Glass /** 50e2dcefcbSMike Frysinger * Access to the OS lseek() system call 51e2dcefcbSMike Frysinger * 52e2dcefcbSMike Frysinger * \param fd File descriptor as returned by os_open() 53e2dcefcbSMike Frysinger * \param offset File offset (based on whence) 54e2dcefcbSMike Frysinger * \param whence Position offset is relative to (see below) 55e2dcefcbSMike Frysinger * \return new file offset 56e2dcefcbSMike Frysinger */ 57e2dcefcbSMike Frysinger off_t os_lseek(int fd, off_t offset, int whence); 58e2dcefcbSMike Frysinger 59e2dcefcbSMike Frysinger /* Defines for "whence" in os_lseek() */ 60e2dcefcbSMike Frysinger #define OS_SEEK_SET 0 61e2dcefcbSMike Frysinger #define OS_SEEK_CUR 1 62e2dcefcbSMike Frysinger #define OS_SEEK_END 2 63e2dcefcbSMike Frysinger 64e2dcefcbSMike Frysinger /** 657a9219c1SSimon Glass * Access to the OS open() system call 667a9219c1SSimon Glass * 677a9219c1SSimon Glass * \param pathname Pathname of file to open 68887bd416SSimon Glass * \param flags Flags, like OS_O_RDONLY, OS_O_RDWR 697a9219c1SSimon Glass * \return file descriptor, or -1 on error 707a9219c1SSimon Glass */ 717a9219c1SSimon Glass int os_open(const char *pathname, int flags); 727a9219c1SSimon Glass 73d9165153SSimon Glass #define OS_O_RDONLY 0 74d9165153SSimon Glass #define OS_O_WRONLY 1 75d9165153SSimon Glass #define OS_O_RDWR 2 76d9165153SSimon Glass #define OS_O_MASK 3 /* Mask for read/write flags */ 77d9165153SSimon Glass #define OS_O_CREAT 0100 78d9165153SSimon Glass 797a9219c1SSimon Glass /** 807a9219c1SSimon Glass * Access to the OS close() system call 817a9219c1SSimon Glass * 827a9219c1SSimon Glass * \param fd File descriptor to close 837a9219c1SSimon Glass * \return 0 on success, -1 on error 847a9219c1SSimon Glass */ 857a9219c1SSimon Glass int os_close(int fd); 867a9219c1SSimon Glass 877a9219c1SSimon Glass /** 88cfd13e8dSStephen Warren * Access to the OS unlink() system call 89cfd13e8dSStephen Warren * 90cfd13e8dSStephen Warren * \param pathname Path of file to delete 91cfd13e8dSStephen Warren * \return 0 for success, other for error 92cfd13e8dSStephen Warren */ 93cfd13e8dSStephen Warren int os_unlink(const char *pathname); 94cfd13e8dSStephen Warren 95cfd13e8dSStephen Warren /** 967a9219c1SSimon Glass * Access to the OS exit() system call 977a9219c1SSimon Glass * 987a9219c1SSimon Glass * This exits with the supplied return code, which should be 0 to indicate 997a9219c1SSimon Glass * success. 1007a9219c1SSimon Glass * 1017a9219c1SSimon Glass * @param exit_code exit code for U-Boot 1027a9219c1SSimon Glass */ 1039d72e67bSMike Frysinger void os_exit(int exit_code) __attribute__((noreturn)); 104ab06a758SMike Frysinger 105ab06a758SMike Frysinger /** 106ab06a758SMike Frysinger * Put tty into raw mode to mimic serial console better 107ffb87905SSimon Glass * 108ffb87905SSimon Glass * @param fd File descriptor of stdin (normally 0) 109ffb87905SSimon Glass * @param allow_sigs Allow Ctrl-C, Ctrl-Z to generate signals rather than 110ffb87905SSimon Glass * be handled by U-Boot 111ab06a758SMike Frysinger */ 112ffb87905SSimon Glass void os_tty_raw(int fd, bool allow_sigs); 11321899b10SMatthias Weisser 11421899b10SMatthias Weisser /** 1158939df09SSimon Glass * Restore the tty to its original mode 1168939df09SSimon Glass * 1178939df09SSimon Glass * Call this to restore the original terminal mode, after it has been changed 1188939df09SSimon Glass * by os_tty_raw(). This is an internal function. 1198939df09SSimon Glass */ 1208939df09SSimon Glass void os_fd_restore(void); 1218939df09SSimon Glass 1228939df09SSimon Glass /** 12321899b10SMatthias Weisser * Acquires some memory from the underlying os. 12421899b10SMatthias Weisser * 12521899b10SMatthias Weisser * \param length Number of bytes to be allocated 12621899b10SMatthias Weisser * \return Pointer to length bytes or NULL on error 12721899b10SMatthias Weisser */ 12821899b10SMatthias Weisser void *os_malloc(size_t length); 129d99a6874SMatthias Weisser 130d99a6874SMatthias Weisser /** 13177595c6dSSimon Glass * Free memory previous allocated with os_malloc()/os_realloc() 13277595c6dSSimon Glass * 13377595c6dSSimon Glass * This returns the memory to the OS. 13477595c6dSSimon Glass * 13577595c6dSSimon Glass * \param ptr Pointer to memory block to free 13677595c6dSSimon Glass */ 137347d06deSMasahiro Yamada void os_free(void *ptr); 13877595c6dSSimon Glass 13977595c6dSSimon Glass /** 14077595c6dSSimon Glass * Reallocate previously-allocated memory to increase/decrease space 14177595c6dSSimon Glass * 14277595c6dSSimon Glass * This works in a similar way to the C library realloc() function. If 14377595c6dSSimon Glass * length is 0, then ptr is freed. Otherwise the space used by ptr is 14477595c6dSSimon Glass * expanded or reduced depending on whether length is larger or smaller 14577595c6dSSimon Glass * than before. 14677595c6dSSimon Glass * 14777595c6dSSimon Glass * If ptr is NULL, then this is similar to calling os_malloc(). 14877595c6dSSimon Glass * 14977595c6dSSimon Glass * This function may need to move the memory block to make room for any 15077595c6dSSimon Glass * extra space, in which case the new pointer is returned. 15177595c6dSSimon Glass * 15277595c6dSSimon Glass * \param ptr Pointer to memory block to reallocate 15377595c6dSSimon Glass * \param length New length for memory block 15477595c6dSSimon Glass * \return pointer to new memory block, or NULL on failure or if length 15577595c6dSSimon Glass * is 0. 15677595c6dSSimon Glass */ 15777595c6dSSimon Glass void *os_realloc(void *ptr, size_t length); 15877595c6dSSimon Glass 15977595c6dSSimon Glass /** 160d99a6874SMatthias Weisser * Access to the usleep function of the os 161d99a6874SMatthias Weisser * 162d99a6874SMatthias Weisser * \param usec Time to sleep in micro seconds 163d99a6874SMatthias Weisser */ 164d99a6874SMatthias Weisser void os_usleep(unsigned long usec); 165d99a6874SMatthias Weisser 166d99a6874SMatthias Weisser /** 167d99a6874SMatthias Weisser * Gets a monotonic increasing number of nano seconds from the OS 168d99a6874SMatthias Weisser * 169d99a6874SMatthias Weisser * \return A monotonic increasing time scaled in nano seconds 170d99a6874SMatthias Weisser */ 1712a54d159SSimon Glass uint64_t os_get_nsec(void); 1724f345d56SMike Frysinger 17370db4212SSimon Glass /** 17470db4212SSimon Glass * Parse arguments and update sandbox state. 17570db4212SSimon Glass * 17670db4212SSimon Glass * @param state Sandbox state to update 17770db4212SSimon Glass * @param argc Argument count 17870db4212SSimon Glass * @param argv Argument vector 17970db4212SSimon Glass * @return 0 if ok, and program should continue; 18070db4212SSimon Glass * 1 if ok, but program should stop; 18170db4212SSimon Glass * -1 on error: program should terminate. 18270db4212SSimon Glass */ 18370db4212SSimon Glass int os_parse_args(struct sandbox_state *state, int argc, char *argv[]); 18470db4212SSimon Glass 18562584db1SSimon Glass /* 18662584db1SSimon Glass * Types of directory entry that we support. See also os_dirent_typename in 18762584db1SSimon Glass * the C file. 18862584db1SSimon Glass */ 18962584db1SSimon Glass enum os_dirent_t { 19062584db1SSimon Glass OS_FILET_REG, /* Regular file */ 19162584db1SSimon Glass OS_FILET_LNK, /* Symbolic link */ 19262584db1SSimon Glass OS_FILET_DIR, /* Directory */ 19362584db1SSimon Glass OS_FILET_UNKNOWN, /* Something else */ 19462584db1SSimon Glass 19562584db1SSimon Glass OS_FILET_COUNT, 19662584db1SSimon Glass }; 19762584db1SSimon Glass 19862584db1SSimon Glass /** A directory entry node, containing information about a single dirent */ 19962584db1SSimon Glass struct os_dirent_node { 20062584db1SSimon Glass struct os_dirent_node *next; /* Pointer to next node, or NULL */ 20162584db1SSimon Glass ulong size; /* Size of file in bytes */ 20262584db1SSimon Glass enum os_dirent_t type; /* Type of entry */ 20362584db1SSimon Glass char name[0]; /* Name of entry */ 20462584db1SSimon Glass }; 20562584db1SSimon Glass 20662584db1SSimon Glass /** 20762584db1SSimon Glass * Get a directionry listing 20862584db1SSimon Glass * 20962584db1SSimon Glass * This allocates and returns a linked list containing the directory listing. 21062584db1SSimon Glass * 21162584db1SSimon Glass * @param dirname Directory to examine 21262584db1SSimon Glass * @param headp Returns pointer to head of linked list, or NULL if none 21362584db1SSimon Glass * @return 0 if ok, -ve on error 21462584db1SSimon Glass */ 21562584db1SSimon Glass int os_dirent_ls(const char *dirname, struct os_dirent_node **headp); 21662584db1SSimon Glass 21762584db1SSimon Glass /** 218*86167089SStefan Brüns * Free directory list 219*86167089SStefan Brüns * 220*86167089SStefan Brüns * This frees a linked list containing a directory listing. 221*86167089SStefan Brüns * 222*86167089SStefan Brüns * @param node Pointer to head of linked list 223*86167089SStefan Brüns */ 224*86167089SStefan Brüns void os_dirent_free(struct os_dirent_node *node); 225*86167089SStefan Brüns 226*86167089SStefan Brüns /** 22762584db1SSimon Glass * Get the name of a directory entry type 22862584db1SSimon Glass * 229*86167089SStefan Brüns * @param type Type to check 23062584db1SSimon Glass * @return string containing the name of that type, or "???" if none/invalid 23162584db1SSimon Glass */ 23262584db1SSimon Glass const char *os_dirent_get_typename(enum os_dirent_t type); 23362584db1SSimon Glass 23462584db1SSimon Glass /** 23562584db1SSimon Glass * Get the size of a file 23662584db1SSimon Glass * 23762584db1SSimon Glass * @param fname Filename to check 23896b1046dSSuriyan Ramasami * @param size size of file is returned if no error 23996b1046dSSuriyan Ramasami * @return 0 on success or -1 if an error ocurred 24062584db1SSimon Glass */ 24196b1046dSSuriyan Ramasami int os_get_filesize(const char *fname, loff_t *size); 24262584db1SSimon Glass 24391b136c7SSimon Glass /** 2445c2859cdSSimon Glass * Write the sandbox RAM buffer to a existing file 2455c2859cdSSimon Glass * 2465c2859cdSSimon Glass * @param fname Filename to write memory to (simple binary format) 2475c2859cdSSimon Glass * @return 0 if OK, -ve on error 2485c2859cdSSimon Glass */ 2495c2859cdSSimon Glass int os_write_ram_buf(const char *fname); 2505c2859cdSSimon Glass 2515c2859cdSSimon Glass /** 2525c2859cdSSimon Glass * Read the sandbox RAM buffer from an existing file 2535c2859cdSSimon Glass * 2545c2859cdSSimon Glass * @param fname Filename containing memory (simple binary format) 2555c2859cdSSimon Glass * @return 0 if OK, -ve on error 2565c2859cdSSimon Glass */ 2575c2859cdSSimon Glass int os_read_ram_buf(const char *fname); 2585c2859cdSSimon Glass 25947f5fcfbSSimon Glass /** 26047f5fcfbSSimon Glass * Jump to a new executable image 26147f5fcfbSSimon Glass * 26247f5fcfbSSimon Glass * This uses exec() to run a new executable image, after putting it in a 26347f5fcfbSSimon Glass * temporary file. The same arguments and environment are passed to this 26447f5fcfbSSimon Glass * new image, with the addition of: 26547f5fcfbSSimon Glass * 26647f5fcfbSSimon Glass * -j <filename> Specifies the filename the image was written to. The 26747f5fcfbSSimon Glass * calling image may want to delete this at some point. 26847f5fcfbSSimon Glass * -m <filename> Specifies the file containing the sandbox memory 26947f5fcfbSSimon Glass * (ram_buf) from this image, so that the new image can 27047f5fcfbSSimon Glass * have access to this. It also means that the original 27147f5fcfbSSimon Glass * memory filename passed to U-Boot will be left intact. 27247f5fcfbSSimon Glass * 27347f5fcfbSSimon Glass * @param dest Buffer containing executable image 27447f5fcfbSSimon Glass * @param size Size of buffer 27547f5fcfbSSimon Glass */ 27647f5fcfbSSimon Glass int os_jump_to_image(const void *dest, int size); 27747f5fcfbSSimon Glass 27894eefdeeSSimon Glass /** 279d4e33f5aSSimon Glass * os_find_u_boot() - Determine the path to U-Boot proper 280d4e33f5aSSimon Glass * 281d4e33f5aSSimon Glass * This function is intended to be called from within sandbox SPL. It uses 282d4e33f5aSSimon Glass * a few heuristics to find U-Boot proper. Normally it is either in the same 283d4e33f5aSSimon Glass * directory, or the directory above (since u-boot-spl is normally in an 284d4e33f5aSSimon Glass * spl/ subdirectory when built). 285d4e33f5aSSimon Glass * 286d4e33f5aSSimon Glass * @fname: Place to put full path to U-Boot 287d4e33f5aSSimon Glass * @maxlen: Maximum size of @fname 288d4e33f5aSSimon Glass * @return 0 if OK, -NOSPC if the filename is too large, -ENOENT if not found 289d4e33f5aSSimon Glass */ 290d4e33f5aSSimon Glass int os_find_u_boot(char *fname, int maxlen); 291d4e33f5aSSimon Glass 292d4e33f5aSSimon Glass /** 293d4e33f5aSSimon Glass * os_spl_to_uboot() - Run U-Boot proper 294d4e33f5aSSimon Glass * 295d4e33f5aSSimon Glass * When called from SPL, this runs U-Boot proper. The filename is obtained by 296d4e33f5aSSimon Glass * calling os_find_u_boot(). 297d4e33f5aSSimon Glass * 298d4e33f5aSSimon Glass * @fname: Full pathname to U-Boot executable 299d4e33f5aSSimon Glass * @return 0 if OK, -ve on error 300d4e33f5aSSimon Glass */ 301d4e33f5aSSimon Glass int os_spl_to_uboot(const char *fname); 302d4e33f5aSSimon Glass 303d4e33f5aSSimon Glass /** 30494eefdeeSSimon Glass * Read the current system time 30594eefdeeSSimon Glass * 30694eefdeeSSimon Glass * This reads the current Local Time and places it into the provided 30794eefdeeSSimon Glass * structure. 30894eefdeeSSimon Glass * 30994eefdeeSSimon Glass * @param rt Place to put system time 31094eefdeeSSimon Glass */ 31194eefdeeSSimon Glass void os_localtime(struct rtc_time *rt); 31294eefdeeSSimon Glass 3134f345d56SMike Frysinger #endif 314