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 1670db4212SSimon Glass struct sandbox_state; 1770db4212SSimon Glass 187a9219c1SSimon Glass /** 197a9219c1SSimon Glass * Access to the OS read() system call 207a9219c1SSimon Glass * 217a9219c1SSimon Glass * \param fd File descriptor as returned by os_open() 227a9219c1SSimon Glass * \param buf Buffer to place data 237a9219c1SSimon Glass * \param count Number of bytes to read 247a9219c1SSimon Glass * \return number of bytes read, or -1 on error 257a9219c1SSimon Glass */ 267a9219c1SSimon Glass ssize_t os_read(int fd, void *buf, size_t count); 277a9219c1SSimon Glass 287a9219c1SSimon Glass /** 29e101550aSTaylor Hutt * Access to the OS read() system call with non-blocking access 30e101550aSTaylor Hutt * 31e101550aSTaylor Hutt * \param fd File descriptor as returned by os_open() 32e101550aSTaylor Hutt * \param buf Buffer to place data 33e101550aSTaylor Hutt * \param count Number of bytes to read 34e101550aSTaylor Hutt * \return number of bytes read, or -1 on error 35e101550aSTaylor Hutt */ 36e101550aSTaylor Hutt ssize_t os_read_no_block(int fd, void *buf, size_t count); 37e101550aSTaylor Hutt 38e101550aSTaylor Hutt /** 397a9219c1SSimon Glass * Access to the OS write() system call 407a9219c1SSimon Glass * 417a9219c1SSimon Glass * \param fd File descriptor as returned by os_open() 427a9219c1SSimon Glass * \param buf Buffer containing data to write 437a9219c1SSimon Glass * \param count Number of bytes to write 447a9219c1SSimon Glass * \return number of bytes written, or -1 on error 457a9219c1SSimon Glass */ 467a9219c1SSimon Glass ssize_t os_write(int fd, const void *buf, size_t count); 477a9219c1SSimon Glass 487a9219c1SSimon Glass /** 49e2dcefcbSMike Frysinger * Access to the OS lseek() system call 50e2dcefcbSMike Frysinger * 51e2dcefcbSMike Frysinger * \param fd File descriptor as returned by os_open() 52e2dcefcbSMike Frysinger * \param offset File offset (based on whence) 53e2dcefcbSMike Frysinger * \param whence Position offset is relative to (see below) 54e2dcefcbSMike Frysinger * \return new file offset 55e2dcefcbSMike Frysinger */ 56e2dcefcbSMike Frysinger off_t os_lseek(int fd, off_t offset, int whence); 57e2dcefcbSMike Frysinger 58e2dcefcbSMike Frysinger /* Defines for "whence" in os_lseek() */ 59e2dcefcbSMike Frysinger #define OS_SEEK_SET 0 60e2dcefcbSMike Frysinger #define OS_SEEK_CUR 1 61e2dcefcbSMike Frysinger #define OS_SEEK_END 2 62e2dcefcbSMike Frysinger 63e2dcefcbSMike Frysinger /** 647a9219c1SSimon Glass * Access to the OS open() system call 657a9219c1SSimon Glass * 667a9219c1SSimon Glass * \param pathname Pathname of file to open 677a9219c1SSimon Glass * \param flags Flags, like O_RDONLY, O_RDWR 687a9219c1SSimon Glass * \return file descriptor, or -1 on error 697a9219c1SSimon Glass */ 707a9219c1SSimon Glass int os_open(const char *pathname, int flags); 717a9219c1SSimon Glass 72d9165153SSimon Glass #define OS_O_RDONLY 0 73d9165153SSimon Glass #define OS_O_WRONLY 1 74d9165153SSimon Glass #define OS_O_RDWR 2 75d9165153SSimon Glass #define OS_O_MASK 3 /* Mask for read/write flags */ 76d9165153SSimon Glass #define OS_O_CREAT 0100 77d9165153SSimon Glass 787a9219c1SSimon Glass /** 797a9219c1SSimon Glass * Access to the OS close() system call 807a9219c1SSimon Glass * 817a9219c1SSimon Glass * \param fd File descriptor to close 827a9219c1SSimon Glass * \return 0 on success, -1 on error 837a9219c1SSimon Glass */ 847a9219c1SSimon Glass int os_close(int fd); 857a9219c1SSimon Glass 867a9219c1SSimon Glass /** 877a9219c1SSimon Glass * Access to the OS exit() system call 887a9219c1SSimon Glass * 897a9219c1SSimon Glass * This exits with the supplied return code, which should be 0 to indicate 907a9219c1SSimon Glass * success. 917a9219c1SSimon Glass * 927a9219c1SSimon Glass * @param exit_code exit code for U-Boot 937a9219c1SSimon Glass */ 949d72e67bSMike Frysinger void os_exit(int exit_code) __attribute__((noreturn)); 95ab06a758SMike Frysinger 96ab06a758SMike Frysinger /** 97ab06a758SMike Frysinger * Put tty into raw mode to mimic serial console better 98ab06a758SMike Frysinger */ 99ab06a758SMike Frysinger void os_tty_raw(int fd); 10021899b10SMatthias Weisser 10121899b10SMatthias Weisser /** 10221899b10SMatthias Weisser * Acquires some memory from the underlying os. 10321899b10SMatthias Weisser * 10421899b10SMatthias Weisser * \param length Number of bytes to be allocated 10521899b10SMatthias Weisser * \return Pointer to length bytes or NULL on error 10621899b10SMatthias Weisser */ 10721899b10SMatthias Weisser void *os_malloc(size_t length); 108d99a6874SMatthias Weisser 109d99a6874SMatthias Weisser /** 11077595c6dSSimon Glass * Free memory previous allocated with os_malloc()/os_realloc() 11177595c6dSSimon Glass * 11277595c6dSSimon Glass * This returns the memory to the OS. 11377595c6dSSimon Glass * 11477595c6dSSimon Glass * \param ptr Pointer to memory block to free 11577595c6dSSimon Glass */ 11677595c6dSSimon Glass void *os_free(void *ptr); 11777595c6dSSimon Glass 11877595c6dSSimon Glass /** 11977595c6dSSimon Glass * Reallocate previously-allocated memory to increase/decrease space 12077595c6dSSimon Glass * 12177595c6dSSimon Glass * This works in a similar way to the C library realloc() function. If 12277595c6dSSimon Glass * length is 0, then ptr is freed. Otherwise the space used by ptr is 12377595c6dSSimon Glass * expanded or reduced depending on whether length is larger or smaller 12477595c6dSSimon Glass * than before. 12577595c6dSSimon Glass * 12677595c6dSSimon Glass * If ptr is NULL, then this is similar to calling os_malloc(). 12777595c6dSSimon Glass * 12877595c6dSSimon Glass * This function may need to move the memory block to make room for any 12977595c6dSSimon Glass * extra space, in which case the new pointer is returned. 13077595c6dSSimon Glass * 13177595c6dSSimon Glass * \param ptr Pointer to memory block to reallocate 13277595c6dSSimon Glass * \param length New length for memory block 13377595c6dSSimon Glass * \return pointer to new memory block, or NULL on failure or if length 13477595c6dSSimon Glass * is 0. 13577595c6dSSimon Glass */ 13677595c6dSSimon Glass void *os_realloc(void *ptr, size_t length); 13777595c6dSSimon Glass 13877595c6dSSimon Glass /** 139d99a6874SMatthias Weisser * Access to the usleep function of the os 140d99a6874SMatthias Weisser * 141d99a6874SMatthias Weisser * \param usec Time to sleep in micro seconds 142d99a6874SMatthias Weisser */ 143d99a6874SMatthias Weisser void os_usleep(unsigned long usec); 144d99a6874SMatthias Weisser 145d99a6874SMatthias Weisser /** 146d99a6874SMatthias Weisser * Gets a monotonic increasing number of nano seconds from the OS 147d99a6874SMatthias Weisser * 148d99a6874SMatthias Weisser * \return A monotonic increasing time scaled in nano seconds 149d99a6874SMatthias Weisser */ 1502a54d159SSimon Glass uint64_t os_get_nsec(void); 1514f345d56SMike Frysinger 15270db4212SSimon Glass /** 15370db4212SSimon Glass * Parse arguments and update sandbox state. 15470db4212SSimon Glass * 15570db4212SSimon Glass * @param state Sandbox state to update 15670db4212SSimon Glass * @param argc Argument count 15770db4212SSimon Glass * @param argv Argument vector 15870db4212SSimon Glass * @return 0 if ok, and program should continue; 15970db4212SSimon Glass * 1 if ok, but program should stop; 16070db4212SSimon Glass * -1 on error: program should terminate. 16170db4212SSimon Glass */ 16270db4212SSimon Glass int os_parse_args(struct sandbox_state *state, int argc, char *argv[]); 16370db4212SSimon Glass 16462584db1SSimon Glass /* 16562584db1SSimon Glass * Types of directory entry that we support. See also os_dirent_typename in 16662584db1SSimon Glass * the C file. 16762584db1SSimon Glass */ 16862584db1SSimon Glass enum os_dirent_t { 16962584db1SSimon Glass OS_FILET_REG, /* Regular file */ 17062584db1SSimon Glass OS_FILET_LNK, /* Symbolic link */ 17162584db1SSimon Glass OS_FILET_DIR, /* Directory */ 17262584db1SSimon Glass OS_FILET_UNKNOWN, /* Something else */ 17362584db1SSimon Glass 17462584db1SSimon Glass OS_FILET_COUNT, 17562584db1SSimon Glass }; 17662584db1SSimon Glass 17762584db1SSimon Glass /** A directory entry node, containing information about a single dirent */ 17862584db1SSimon Glass struct os_dirent_node { 17962584db1SSimon Glass struct os_dirent_node *next; /* Pointer to next node, or NULL */ 18062584db1SSimon Glass ulong size; /* Size of file in bytes */ 18162584db1SSimon Glass enum os_dirent_t type; /* Type of entry */ 18262584db1SSimon Glass char name[0]; /* Name of entry */ 18362584db1SSimon Glass }; 18462584db1SSimon Glass 18562584db1SSimon Glass /** 18662584db1SSimon Glass * Get a directionry listing 18762584db1SSimon Glass * 18862584db1SSimon Glass * This allocates and returns a linked list containing the directory listing. 18962584db1SSimon Glass * 19062584db1SSimon Glass * @param dirname Directory to examine 19162584db1SSimon Glass * @param headp Returns pointer to head of linked list, or NULL if none 19262584db1SSimon Glass * @return 0 if ok, -ve on error 19362584db1SSimon Glass */ 19462584db1SSimon Glass int os_dirent_ls(const char *dirname, struct os_dirent_node **headp); 19562584db1SSimon Glass 19662584db1SSimon Glass /** 19762584db1SSimon Glass * Get the name of a directory entry type 19862584db1SSimon Glass * 19962584db1SSimon Glass * @param type Type to cehck 20062584db1SSimon Glass * @return string containing the name of that type, or "???" if none/invalid 20162584db1SSimon Glass */ 20262584db1SSimon Glass const char *os_dirent_get_typename(enum os_dirent_t type); 20362584db1SSimon Glass 20462584db1SSimon Glass /** 20562584db1SSimon Glass * Get the size of a file 20662584db1SSimon Glass * 20762584db1SSimon Glass * @param fname Filename to check 20862584db1SSimon Glass * @return size of file, or -1 if an error ocurred 20962584db1SSimon Glass */ 21062584db1SSimon Glass ssize_t os_get_filesize(const char *fname); 21162584db1SSimon Glass 21291b136c7SSimon Glass /** 21391b136c7SSimon Glass * Write a character to the controlling OS terminal 21491b136c7SSimon Glass * 21591b136c7SSimon Glass * This bypasses the U-Boot console support and writes directly to the OS 21691b136c7SSimon Glass * stdout file descriptor. 21791b136c7SSimon Glass * 21891b136c7SSimon Glass * @param ch Character to write 21991b136c7SSimon Glass */ 22091b136c7SSimon Glass void os_putc(int ch); 22191b136c7SSimon Glass 22291b136c7SSimon Glass /** 22391b136c7SSimon Glass * Write a string to the controlling OS terminal 22491b136c7SSimon Glass * 22591b136c7SSimon Glass * This bypasses the U-Boot console support and writes directly to the OS 22691b136c7SSimon Glass * stdout file descriptor. 22791b136c7SSimon Glass * 22891b136c7SSimon Glass * @param str String to write (note that \n is not appended) 22991b136c7SSimon Glass */ 23091b136c7SSimon Glass void os_puts(const char *str); 23191b136c7SSimon Glass 232*5c2859cdSSimon Glass /** 233*5c2859cdSSimon Glass * Write the sandbox RAM buffer to a existing file 234*5c2859cdSSimon Glass * 235*5c2859cdSSimon Glass * @param fname Filename to write memory to (simple binary format) 236*5c2859cdSSimon Glass * @return 0 if OK, -ve on error 237*5c2859cdSSimon Glass */ 238*5c2859cdSSimon Glass int os_write_ram_buf(const char *fname); 239*5c2859cdSSimon Glass 240*5c2859cdSSimon Glass /** 241*5c2859cdSSimon Glass * Read the sandbox RAM buffer from an existing file 242*5c2859cdSSimon Glass * 243*5c2859cdSSimon Glass * @param fname Filename containing memory (simple binary format) 244*5c2859cdSSimon Glass * @return 0 if OK, -ve on error 245*5c2859cdSSimon Glass */ 246*5c2859cdSSimon Glass int os_read_ram_buf(const char *fname); 247*5c2859cdSSimon Glass 2484f345d56SMike Frysinger #endif 249