efi_file.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * EFI utils
  4. *
  5. * Copyright (c) 2017 Rob Clark
  6. */
  7. #include <common.h>
  8. #include <charset.h>
  9. #include <efi_loader.h>
  10. #include <malloc.h>
  11. #include <mapmem.h>
  12. #include <fs.h>
  13. /* GUID for file system information */
  14. const efi_guid_t efi_file_system_info_guid = EFI_FILE_SYSTEM_INFO_GUID;
  15. struct file_system {
  16. struct efi_simple_file_system_protocol base;
  17. struct efi_device_path *dp;
  18. struct blk_desc *desc;
  19. int part;
  20. };
  21. #define to_fs(x) container_of(x, struct file_system, base)
  22. struct file_handle {
  23. struct efi_file_handle base;
  24. struct file_system *fs;
  25. loff_t offset; /* current file position/cursor */
  26. int isdir;
  27. /* for reading a directory: */
  28. struct fs_dir_stream *dirs;
  29. struct fs_dirent *dent;
  30. char path[0];
  31. };
  32. #define to_fh(x) container_of(x, struct file_handle, base)
  33. static const struct efi_file_handle efi_file_handle_protocol;
  34. static char *basename(struct file_handle *fh)
  35. {
  36. char *s = strrchr(fh->path, '/');
  37. if (s)
  38. return s + 1;
  39. return fh->path;
  40. }
  41. static int set_blk_dev(struct file_handle *fh)
  42. {
  43. return fs_set_blk_dev_with_part(fh->fs->desc, fh->fs->part);
  44. }
  45. static int is_dir(struct file_handle *fh)
  46. {
  47. struct fs_dir_stream *dirs;
  48. set_blk_dev(fh);
  49. dirs = fs_opendir(fh->path);
  50. if (!dirs)
  51. return 0;
  52. fs_closedir(dirs);
  53. return 1;
  54. }
  55. /*
  56. * Normalize a path which may include either back or fwd slashes,
  57. * double slashes, . or .. entries in the path, etc.
  58. */
  59. static int sanitize_path(char *path)
  60. {
  61. char *p;
  62. /* backslash to slash: */
  63. p = path;
  64. while ((p = strchr(p, '\\')))
  65. *p++ = '/';
  66. /* handle double-slashes: */
  67. p = path;
  68. while ((p = strstr(p, "//"))) {
  69. char *src = p + 1;
  70. memmove(p, src, strlen(src) + 1);
  71. }
  72. /* handle extra /.'s */
  73. p = path;
  74. while ((p = strstr(p, "/."))) {
  75. /*
  76. * You'd be tempted to do this *after* handling ".."s
  77. * below to avoid having to check if "/." is start of
  78. * a "/..", but that won't have the correct results..
  79. * for example, "/foo/./../bar" would get resolved to
  80. * "/foo/bar" if you did these two passes in the other
  81. * order
  82. */
  83. if (p[2] == '.') {
  84. p += 2;
  85. continue;
  86. }
  87. char *src = p + 2;
  88. memmove(p, src, strlen(src) + 1);
  89. }
  90. /* handle extra /..'s: */
  91. p = path;
  92. while ((p = strstr(p, "/.."))) {
  93. char *src = p + 3;
  94. p--;
  95. /* find beginning of previous path entry: */
  96. while (true) {
  97. if (p < path)
  98. return -1;
  99. if (*p == '/')
  100. break;
  101. p--;
  102. }
  103. memmove(p, src, strlen(src) + 1);
  104. }
  105. return 0;
  106. }
  107. /* NOTE: despite what you would expect, 'file_name' is actually a path.
  108. * With windoze style backlashes, ofc.
  109. */
  110. static struct efi_file_handle *file_open(struct file_system *fs,
  111. struct file_handle *parent, s16 *file_name, u64 mode)
  112. {
  113. struct file_handle *fh;
  114. char f0[MAX_UTF8_PER_UTF16] = {0};
  115. int plen = 0;
  116. int flen = 0;
  117. if (file_name) {
  118. utf16_to_utf8((u8 *)f0, (u16 *)file_name, 1);
  119. flen = utf16_strlen((u16 *)file_name);
  120. }
  121. /* we could have a parent, but also an absolute path: */
  122. if (f0[0] == '\\') {
  123. plen = 0;
  124. } else if (parent) {
  125. plen = strlen(parent->path) + 1;
  126. }
  127. /* +2 is for null and '/' */
  128. fh = calloc(1, sizeof(*fh) + plen + (flen * MAX_UTF8_PER_UTF16) + 2);
  129. fh->base = efi_file_handle_protocol;
  130. fh->fs = fs;
  131. if (parent) {
  132. char *p = fh->path;
  133. if (plen > 0) {
  134. strcpy(p, parent->path);
  135. p += plen - 1;
  136. *p++ = '/';
  137. }
  138. utf16_to_utf8((u8 *)p, (u16 *)file_name, flen);
  139. if (sanitize_path(fh->path))
  140. goto error;
  141. /* check if file exists: */
  142. if (set_blk_dev(fh))
  143. goto error;
  144. if (!((mode & EFI_FILE_MODE_CREATE) || fs_exists(fh->path)))
  145. goto error;
  146. /* figure out if file is a directory: */
  147. fh->isdir = is_dir(fh);
  148. } else {
  149. fh->isdir = 1;
  150. strcpy(fh->path, "");
  151. }
  152. return &fh->base;
  153. error:
  154. free(fh);
  155. return NULL;
  156. }
  157. static efi_status_t EFIAPI efi_file_open(struct efi_file_handle *file,
  158. struct efi_file_handle **new_handle,
  159. s16 *file_name, u64 open_mode, u64 attributes)
  160. {
  161. struct file_handle *fh = to_fh(file);
  162. EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu", file, new_handle, file_name,
  163. open_mode, attributes);
  164. *new_handle = file_open(fh->fs, fh, file_name, open_mode);
  165. if (!*new_handle)
  166. return EFI_EXIT(EFI_NOT_FOUND);
  167. return EFI_EXIT(EFI_SUCCESS);
  168. }
  169. static efi_status_t file_close(struct file_handle *fh)
  170. {
  171. fs_closedir(fh->dirs);
  172. free(fh);
  173. return EFI_SUCCESS;
  174. }
  175. static efi_status_t EFIAPI efi_file_close(struct efi_file_handle *file)
  176. {
  177. struct file_handle *fh = to_fh(file);
  178. EFI_ENTRY("%p", file);
  179. return EFI_EXIT(file_close(fh));
  180. }
  181. static efi_status_t EFIAPI efi_file_delete(struct efi_file_handle *file)
  182. {
  183. struct file_handle *fh = to_fh(file);
  184. EFI_ENTRY("%p", file);
  185. file_close(fh);
  186. return EFI_EXIT(EFI_WARN_DELETE_FAILURE);
  187. }
  188. static efi_status_t file_read(struct file_handle *fh, u64 *buffer_size,
  189. void *buffer)
  190. {
  191. loff_t actread;
  192. /* fs_read expects buffer address, not pointer */
  193. uintptr_t buffer_addr = (uintptr_t)map_to_sysmem(buffer);
  194. if (fs_read(fh->path, buffer_addr, fh->offset,
  195. *buffer_size, &actread))
  196. return EFI_DEVICE_ERROR;
  197. *buffer_size = actread;
  198. fh->offset += actread;
  199. return EFI_SUCCESS;
  200. }
  201. static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size,
  202. void *buffer)
  203. {
  204. struct efi_file_info *info = buffer;
  205. struct fs_dirent *dent;
  206. unsigned int required_size;
  207. if (!fh->dirs) {
  208. assert(fh->offset == 0);
  209. fh->dirs = fs_opendir(fh->path);
  210. if (!fh->dirs)
  211. return EFI_DEVICE_ERROR;
  212. }
  213. /*
  214. * So this is a bit awkward. Since fs layer is stateful and we
  215. * can't rewind an entry, in the EFI_BUFFER_TOO_SMALL case below
  216. * we might have to return without consuming the dent.. so we
  217. * have to stash it for next call.
  218. */
  219. if (fh->dent) {
  220. dent = fh->dent;
  221. fh->dent = NULL;
  222. } else {
  223. dent = fs_readdir(fh->dirs);
  224. }
  225. if (!dent) {
  226. /* no more files in directory: */
  227. /* workaround shim.efi bug/quirk.. as find_boot_csv()
  228. * loops through directory contents, it initially calls
  229. * read w/ zero length buffer to find out how much mem
  230. * to allocate for the EFI_FILE_INFO, then allocates,
  231. * and then calls a 2nd time. If we return size of
  232. * zero the first time, it happily passes that to
  233. * AllocateZeroPool(), and when that returns NULL it
  234. * thinks it is EFI_OUT_OF_RESOURCES. So on first
  235. * call return a non-zero size:
  236. */
  237. if (*buffer_size == 0)
  238. *buffer_size = sizeof(*info);
  239. else
  240. *buffer_size = 0;
  241. return EFI_SUCCESS;
  242. }
  243. /* check buffer size: */
  244. required_size = sizeof(*info) + 2 * (strlen(dent->name) + 1);
  245. if (*buffer_size < required_size) {
  246. *buffer_size = required_size;
  247. fh->dent = dent;
  248. return EFI_BUFFER_TOO_SMALL;
  249. }
  250. *buffer_size = required_size;
  251. memset(info, 0, required_size);
  252. info->size = required_size;
  253. info->file_size = dent->size;
  254. info->physical_size = dent->size;
  255. if (dent->type == FS_DT_DIR)
  256. info->attribute |= EFI_FILE_DIRECTORY;
  257. ascii2unicode((u16 *)info->file_name, dent->name);
  258. fh->offset++;
  259. return EFI_SUCCESS;
  260. }
  261. static efi_status_t EFIAPI efi_file_read(struct efi_file_handle *file,
  262. efi_uintn_t *buffer_size, void *buffer)
  263. {
  264. struct file_handle *fh = to_fh(file);
  265. efi_status_t ret = EFI_SUCCESS;
  266. u64 bs;
  267. EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
  268. if (!buffer_size || !buffer) {
  269. ret = EFI_INVALID_PARAMETER;
  270. goto error;
  271. }
  272. if (set_blk_dev(fh)) {
  273. ret = EFI_DEVICE_ERROR;
  274. goto error;
  275. }
  276. bs = *buffer_size;
  277. if (fh->isdir)
  278. ret = dir_read(fh, &bs, buffer);
  279. else
  280. ret = file_read(fh, &bs, buffer);
  281. if (bs <= SIZE_MAX)
  282. *buffer_size = bs;
  283. else
  284. *buffer_size = SIZE_MAX;
  285. error:
  286. return EFI_EXIT(ret);
  287. }
  288. static efi_status_t EFIAPI efi_file_write(struct efi_file_handle *file,
  289. efi_uintn_t *buffer_size,
  290. void *buffer)
  291. {
  292. struct file_handle *fh = to_fh(file);
  293. efi_status_t ret = EFI_SUCCESS;
  294. loff_t actwrite;
  295. EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
  296. if (set_blk_dev(fh)) {
  297. ret = EFI_DEVICE_ERROR;
  298. goto error;
  299. }
  300. if (fs_write(fh->path, (ulong)buffer, fh->offset, *buffer_size,
  301. &actwrite)) {
  302. ret = EFI_DEVICE_ERROR;
  303. goto error;
  304. }
  305. *buffer_size = actwrite;
  306. fh->offset += actwrite;
  307. error:
  308. return EFI_EXIT(ret);
  309. }
  310. static efi_status_t EFIAPI efi_file_getpos(struct efi_file_handle *file,
  311. efi_uintn_t *pos)
  312. {
  313. struct file_handle *fh = to_fh(file);
  314. EFI_ENTRY("%p, %p", file, pos);
  315. if (fh->offset <= SIZE_MAX) {
  316. *pos = fh->offset;
  317. return EFI_EXIT(EFI_SUCCESS);
  318. } else {
  319. return EFI_EXIT(EFI_DEVICE_ERROR);
  320. }
  321. }
  322. static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file,
  323. efi_uintn_t pos)
  324. {
  325. struct file_handle *fh = to_fh(file);
  326. efi_status_t ret = EFI_SUCCESS;
  327. EFI_ENTRY("%p, %zu", file, pos);
  328. if (fh->isdir) {
  329. if (pos != 0) {
  330. ret = EFI_UNSUPPORTED;
  331. goto error;
  332. }
  333. fs_closedir(fh->dirs);
  334. fh->dirs = NULL;
  335. }
  336. if (pos == ~0ULL) {
  337. loff_t file_size;
  338. if (set_blk_dev(fh)) {
  339. ret = EFI_DEVICE_ERROR;
  340. goto error;
  341. }
  342. if (fs_size(fh->path, &file_size)) {
  343. ret = EFI_DEVICE_ERROR;
  344. goto error;
  345. }
  346. pos = file_size;
  347. }
  348. fh->offset = pos;
  349. error:
  350. return EFI_EXIT(ret);
  351. }
  352. static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
  353. const efi_guid_t *info_type,
  354. efi_uintn_t *buffer_size,
  355. void *buffer)
  356. {
  357. struct file_handle *fh = to_fh(file);
  358. efi_status_t ret = EFI_SUCCESS;
  359. EFI_ENTRY("%p, %p, %p, %p", file, info_type, buffer_size, buffer);
  360. if (!guidcmp(info_type, &efi_file_info_guid)) {
  361. struct efi_file_info *info = buffer;
  362. char *filename = basename(fh);
  363. unsigned int required_size;
  364. loff_t file_size;
  365. /* check buffer size: */
  366. required_size = sizeof(*info) + 2 * (strlen(filename) + 1);
  367. if (*buffer_size < required_size) {
  368. *buffer_size = required_size;
  369. ret = EFI_BUFFER_TOO_SMALL;
  370. goto error;
  371. }
  372. if (set_blk_dev(fh)) {
  373. ret = EFI_DEVICE_ERROR;
  374. goto error;
  375. }
  376. if (fs_size(fh->path, &file_size)) {
  377. ret = EFI_DEVICE_ERROR;
  378. goto error;
  379. }
  380. memset(info, 0, required_size);
  381. info->size = required_size;
  382. info->file_size = file_size;
  383. info->physical_size = file_size;
  384. if (fh->isdir)
  385. info->attribute |= EFI_FILE_DIRECTORY;
  386. ascii2unicode((u16 *)info->file_name, filename);
  387. } else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
  388. struct efi_file_system_info *info = buffer;
  389. disk_partition_t part;
  390. efi_uintn_t required_size;
  391. int r;
  392. if (fh->fs->part >= 1)
  393. r = part_get_info(fh->fs->desc, fh->fs->part, &part);
  394. else
  395. r = part_get_info_whole_disk(fh->fs->desc, &part);
  396. if (r < 0) {
  397. ret = EFI_DEVICE_ERROR;
  398. goto error;
  399. }
  400. required_size = sizeof(info) + 2 *
  401. (strlen((const char *)part.name) + 1);
  402. if (*buffer_size < required_size) {
  403. *buffer_size = required_size;
  404. ret = EFI_BUFFER_TOO_SMALL;
  405. goto error;
  406. }
  407. memset(info, 0, required_size);
  408. info->size = required_size;
  409. info->read_only = true;
  410. info->volume_size = part.size * part.blksz;
  411. info->free_space = 0;
  412. info->block_size = part.blksz;
  413. /*
  414. * TODO: The volume label is not available in U-Boot.
  415. * Use the partition name as substitute.
  416. */
  417. ascii2unicode((u16 *)info->volume_label,
  418. (const char *)part.name);
  419. } else {
  420. ret = EFI_UNSUPPORTED;
  421. }
  422. error:
  423. return EFI_EXIT(ret);
  424. }
  425. static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
  426. const efi_guid_t *info_type,
  427. efi_uintn_t buffer_size,
  428. void *buffer)
  429. {
  430. EFI_ENTRY("%p, %p, %zu, %p", file, info_type, buffer_size, buffer);
  431. return EFI_EXIT(EFI_UNSUPPORTED);
  432. }
  433. static efi_status_t EFIAPI efi_file_flush(struct efi_file_handle *file)
  434. {
  435. EFI_ENTRY("%p", file);
  436. return EFI_EXIT(EFI_SUCCESS);
  437. }
  438. static const struct efi_file_handle efi_file_handle_protocol = {
  439. .rev = EFI_FILE_PROTOCOL_REVISION,
  440. .open = efi_file_open,
  441. .close = efi_file_close,
  442. .delete = efi_file_delete,
  443. .read = efi_file_read,
  444. .write = efi_file_write,
  445. .getpos = efi_file_getpos,
  446. .setpos = efi_file_setpos,
  447. .getinfo = efi_file_getinfo,
  448. .setinfo = efi_file_setinfo,
  449. .flush = efi_file_flush,
  450. };
  451. struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp)
  452. {
  453. struct efi_simple_file_system_protocol *v;
  454. struct efi_file_handle *f;
  455. efi_status_t ret;
  456. v = efi_fs_from_path(fp);
  457. if (!v)
  458. return NULL;
  459. EFI_CALL(ret = v->open_volume(v, &f));
  460. if (ret != EFI_SUCCESS)
  461. return NULL;
  462. /* skip over device-path nodes before the file path: */
  463. while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH))
  464. fp = efi_dp_next(fp);
  465. while (fp) {
  466. struct efi_device_path_file_path *fdp =
  467. container_of(fp, struct efi_device_path_file_path, dp);
  468. struct efi_file_handle *f2;
  469. if (!EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) {
  470. printf("bad file path!\n");
  471. f->close(f);
  472. return NULL;
  473. }
  474. EFI_CALL(ret = f->open(f, &f2, (s16 *)fdp->str,
  475. EFI_FILE_MODE_READ, 0));
  476. if (ret != EFI_SUCCESS)
  477. return NULL;
  478. fp = efi_dp_next(fp);
  479. EFI_CALL(f->close(f));
  480. f = f2;
  481. }
  482. return f;
  483. }
  484. static efi_status_t EFIAPI
  485. efi_open_volume(struct efi_simple_file_system_protocol *this,
  486. struct efi_file_handle **root)
  487. {
  488. struct file_system *fs = to_fs(this);
  489. EFI_ENTRY("%p, %p", this, root);
  490. *root = file_open(fs, NULL, NULL, 0);
  491. return EFI_EXIT(EFI_SUCCESS);
  492. }
  493. struct efi_simple_file_system_protocol *
  494. efi_simple_file_system(struct blk_desc *desc, int part,
  495. struct efi_device_path *dp)
  496. {
  497. struct file_system *fs;
  498. fs = calloc(1, sizeof(*fs));
  499. fs->base.rev = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION;
  500. fs->base.open_volume = efi_open_volume;
  501. fs->desc = desc;
  502. fs->part = part;
  503. fs->dp = dp;
  504. return &fs->base;
  505. }