unistd.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /* Copyright (C) 2022 Andrius Štikonas
  2. * This file is part of M2-Planet.
  3. *
  4. * M2-Planet is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * M2-Planet is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with M2-Planet. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #ifndef _UNISTD_C
  18. #define _UNISTD_C
  19. #include <uefi/uefi.c>
  20. #include <sys/utsname.h>
  21. #include <stdio.h>
  22. #define NULL 0
  23. #define EOF 0xFFFFFFFF
  24. /* For lseek */
  25. #define SEEK_SET 0
  26. #define SEEK_CUR 1
  27. #define SEEK_END 2
  28. void* malloc(unsigned size);
  29. size_t strlen(char const* str);
  30. char* strncpy(char* dest, char const* src, size_t count);
  31. char* strncat(char* dest, char const* src, size_t count);
  32. void* memcpy(void* dest, void const* src, size_t count);
  33. int open(char* name, int flag, int mode);
  34. int close(int fd);
  35. int access(char* pathname, int mode)
  36. {
  37. int fd = open(pathname, 0, 0);
  38. if (fd == -1)
  39. {
  40. return -1;
  41. }
  42. close(fd);
  43. return 0;
  44. }
  45. int chdir(char* path)
  46. {
  47. if (access(path, 0) == -1)
  48. {
  49. return -1;
  50. }
  51. char* absolute_path = _relative_path_to_absolute(path);
  52. strncpy(_cwd, absolute_path, __PATH_MAX);
  53. if(_cwd[strlen(_cwd) - 1] != '\\')
  54. {
  55. strncat(_cwd, "/", __PATH_MAX);
  56. }
  57. free(absolute_path);
  58. return 0;
  59. }
  60. int fchdir(int fd)
  61. {
  62. /* TODO: not yet implemented. */
  63. return -1;
  64. }
  65. int _get_file_size(struct efi_file_protocol* f)
  66. {
  67. /* Preallocate some extra space for file_name */
  68. size_t file_info_size = sizeof(struct efi_file_info);
  69. struct efi_file_info* file_info = calloc(1, file_info_size);
  70. unsigned rval = __uefi_4(f, &EFI_FILE_INFO_GUID, &file_info_size, file_info, f->get_info);
  71. if(rval != EFI_SUCCESS)
  72. {
  73. return -1;
  74. }
  75. int file_size = file_info->file_size;
  76. free(file_info);
  77. return file_size;
  78. }
  79. void _set_environment(char** envp)
  80. {
  81. unsigned i;
  82. unsigned j;
  83. unsigned length = _array_length(envp);
  84. char* name;
  85. char* value;
  86. for(i = 0; i < length; i += 1)
  87. {
  88. j = 0;
  89. name = envp[i];
  90. while(envp[i][j] != '=')
  91. {
  92. j += 1;
  93. }
  94. envp[i][j] = 0;
  95. value = envp[i] + j + 1;
  96. _set_variable(name, value);
  97. envp[i][j] = '=';
  98. }
  99. }
  100. FILE* fopen(char const* filename, char const* mode);
  101. size_t fread(void* buffer, size_t size, size_t count, FILE* stream);
  102. int fclose(FILE* stream);
  103. int spawn(char* file_name, char** argv, char** envp)
  104. {
  105. FILE* fcmd = fopen(file_name, "r");
  106. if(fcmd == NULL) return -1;
  107. long program_size = _get_file_size(fcmd->fd);
  108. void* executable = malloc(program_size);
  109. size_t count = fread(executable, 1, program_size, fcmd);
  110. if(count < program_size)
  111. {
  112. free(executable);
  113. fclose(fcmd);
  114. return -1;
  115. }
  116. fclose(fcmd);
  117. struct efi_device_path_protocol* device_path = calloc(2, sizeof(struct efi_device_path_protocol));
  118. device_path->type = HARDWARE_DEVICE_PATH;
  119. device_path->subtype = MEMORY_MAPPED;
  120. device_path->length = sizeof(struct efi_device_path_protocol);
  121. device_path->memory_type = EFI_LOADER_DATA;
  122. device_path->start_address = executable;
  123. device_path->end_address = executable + program_size;
  124. device_path[1].type = END_HARDWARE_DEVICE_PATH;
  125. device_path[1].subtype = END_ENTIRE_DEVICE_PATH;
  126. device_path[1].length = 4;
  127. void* child_ih;
  128. unsigned rval = __uefi_6(0, _image_handle, device_path, executable, program_size, &child_ih, _system->boot_services->load_image);
  129. free(device_path);
  130. free(executable);
  131. if(rval != EFI_SUCCESS) return -1;
  132. struct efi_loaded_image_protocol* child_image;
  133. rval = _open_protocol(child_ih, &EFI_LOADED_IMAGE_PROTOCOL_GUID, &child_image, child_ih, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
  134. if(rval != EFI_SUCCESS) return -1;
  135. /* Concatenate char** argv array */
  136. unsigned arg_length = -1 ;
  137. unsigned i = 0;
  138. while(argv[i] != NULL)
  139. {
  140. arg_length += strlen(argv[i]) + 1;
  141. i += 1;
  142. }
  143. char* load_options = calloc(arg_length + 1, 1);
  144. strcpy(load_options, argv[0]);
  145. i = 1;
  146. while(argv[i] != NULL)
  147. {
  148. strcat(load_options, " ");
  149. strcat(load_options, argv[i]);
  150. i += 1;
  151. }
  152. char* uefi_path = _string2wide(load_options);
  153. child_image->load_options = uefi_path;
  154. child_image->load_options_size = 2 * arg_length;
  155. free(load_options);
  156. child_image->device = _image->device;
  157. rval = _close_protocol(child_ih, &EFI_LOADED_IMAGE_PROTOCOL_GUID, child_ih, 0);
  158. if(rval != EFI_SUCCESS) return -1;
  159. /* Setup environment for child process */
  160. _set_environment(envp);
  161. _set_variable("cwd", _cwd);
  162. _set_variable("root", _root);
  163. /* Run command */
  164. rval = __uefi_3(child_ih, 0, 0, _system->boot_services->start_image);
  165. free(uefi_path);
  166. /* Restore initial environment
  167. * For simplicity we just delete all variables and restore them from _envp.
  168. * This assumes that _envp is not modified by application, e.g. kaem.
  169. */
  170. _wipe_environment();
  171. _set_environment(_envp);
  172. return rval;
  173. }
  174. int fork()
  175. {
  176. return -1;
  177. }
  178. int waitpid (int pid, int* status_ptr, int options)
  179. {
  180. return -1;
  181. }
  182. int execve(char* file_name, char** argv, char** envp)
  183. {
  184. return -1;
  185. }
  186. int read(int fd, char* buf, unsigned count)
  187. {
  188. struct efi_file_protocol* f = fd;
  189. __uefi_3(fd, &count, buf, f->read);
  190. return count;
  191. }
  192. int write(int fd, char* buf, unsigned count)
  193. {
  194. struct efi_file_protocol* f = fd;
  195. unsigned i;
  196. char c = 0;
  197. /* In UEFI StdErr might not be printing stuff to console, so just use stdout */
  198. if(f == STDOUT_FILENO || f == STDERR_FILENO)
  199. {
  200. for(i = 0; i < count; i += 1)
  201. {
  202. c = buf[i];
  203. __uefi_2(_system->con_out, &c, _system->con_out->output_string);
  204. if('\n' == c)
  205. {
  206. c = '\r';
  207. __uefi_2(_system->con_out, &c, _system->con_out->output_string);
  208. }
  209. }
  210. return i;
  211. }
  212. /* Otherwise write to file */
  213. __uefi_3(f, &count, buf, f->write);
  214. return count;
  215. }
  216. int lseek(int fd, int offset, int whence)
  217. {
  218. struct efi_file_protocol* f = fd;
  219. if(whence == SEEK_SET)
  220. {
  221. }
  222. else if(whence == SEEK_CUR)
  223. {
  224. unsigned position;
  225. __uefi_2(f, &position, f->get_position);
  226. offset += position;
  227. }
  228. else if(whence == SEEK_END)
  229. {
  230. offset += _get_file_size(fd);
  231. }
  232. else
  233. {
  234. return -1;
  235. }
  236. unsigned rval = __uefi_2(f, offset, f->set_position);
  237. if(rval == EFI_SUCCESS)
  238. {
  239. return offset;
  240. }
  241. return -1;
  242. }
  243. int close(int fd)
  244. {
  245. struct efi_file_protocol* f = fd;
  246. unsigned rval = __uefi_1(f, f->close);
  247. if(rval != EFI_SUCCESS)
  248. {
  249. return -1;
  250. }
  251. return rval;
  252. }
  253. int unlink(char* filename)
  254. {
  255. FILE* f = fopen(filename, "w");
  256. struct efi_file_protocol* fd = f->fd;
  257. __uefi_1(fd, fd->delete);
  258. }
  259. int symlink(char *path1, char *path2)
  260. {
  261. /* This does not make sense in UEFI, where there are no symlinks */
  262. return -1;
  263. }
  264. char* getcwd(char* buf, unsigned size)
  265. {
  266. size_t length = strlen(_cwd);
  267. if(length >= size) return NULL;
  268. strcpy(buf, _cwd);
  269. return buf;
  270. }
  271. char* getwd(char* buf)
  272. {
  273. return getcwd(buf, __PATH_MAX);
  274. }
  275. char* get_current_dir_name()
  276. {
  277. return getcwd(malloc(__PATH_MAX), __PATH_MAX);
  278. }
  279. int brk(void *addr)
  280. {
  281. return -1;
  282. }
  283. int uname(struct utsname* unameData)
  284. {
  285. memcpy(unameData->sysname, "UEFI", 5);
  286. memcpy(unameData->release, "1.0", 4);
  287. memcpy(unameData->version, "1.0", 4);
  288. #ifdef __x86_64__
  289. memcpy(unameData->machine, "x86_64", 7);
  290. #else
  291. #error unsupported arch
  292. #endif
  293. }
  294. int unshare(int flags)
  295. {
  296. if (flags != 0)
  297. {
  298. return -1; // Any unshare operation is invalid
  299. }
  300. return 0;
  301. }
  302. int geteuid(int flags)
  303. {
  304. return 0;
  305. }
  306. int getegid(int flags)
  307. {
  308. return 0;
  309. }
  310. int chroot(char const *path)
  311. {
  312. char *newroot = _relative_path_to_absolute(path);
  313. free(_root);
  314. _root = newroot;
  315. if(_root[strlen(_root) - 1] != '\\')
  316. {
  317. strncat(_root, "/", __PATH_MAX);
  318. }
  319. return 0;
  320. }
  321. int mount(char const *source, char const *target, char const *filesystemtype, SCM mountflags, void const *data)
  322. {
  323. return -1;
  324. }
  325. #endif