efi_file.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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. /**
  108. * file_open() - open a file handle
  109. *
  110. * @fs: file system
  111. * @parent: directory relative to which the file is to be opened
  112. * @file_name: path of the file to be opened. '\', '.', or '..' may
  113. * be used as modifiers. A leading backslash indicates an
  114. * absolute path.
  115. * @mode: bit mask indicating the access mode (read, write,
  116. * create)
  117. * @attributes: attributes for newly created file
  118. * Returns: handle to the opened file or NULL
  119. */
  120. static struct efi_file_handle *file_open(struct file_system *fs,
  121. struct file_handle *parent, s16 *file_name, u64 mode,
  122. u64 attributes)
  123. {
  124. struct file_handle *fh;
  125. char f0[MAX_UTF8_PER_UTF16] = {0};
  126. int plen = 0;
  127. int flen = 0;
  128. if (file_name) {
  129. utf16_to_utf8((u8 *)f0, (u16 *)file_name, 1);
  130. flen = u16_strlen((u16 *)file_name);
  131. }
  132. /* we could have a parent, but also an absolute path: */
  133. if (f0[0] == '\\') {
  134. plen = 0;
  135. } else if (parent) {
  136. plen = strlen(parent->path) + 1;
  137. }
  138. /* +2 is for null and '/' */
  139. fh = calloc(1, sizeof(*fh) + plen + (flen * MAX_UTF8_PER_UTF16) + 2);
  140. fh->base = efi_file_handle_protocol;
  141. fh->fs = fs;
  142. if (parent) {
  143. char *p = fh->path;
  144. if (plen > 0) {
  145. strcpy(p, parent->path);
  146. p += plen - 1;
  147. *p++ = '/';
  148. }
  149. utf16_to_utf8((u8 *)p, (u16 *)file_name, flen);
  150. if (sanitize_path(fh->path))
  151. goto error;
  152. /* check if file exists: */
  153. if (set_blk_dev(fh))
  154. goto error;
  155. if ((mode & EFI_FILE_MODE_CREATE) &&
  156. (attributes & EFI_FILE_DIRECTORY)) {
  157. if (fs_mkdir(fh->path))
  158. goto error;
  159. } else if (!((mode & EFI_FILE_MODE_CREATE) ||
  160. fs_exists(fh->path)))
  161. goto error;
  162. /* figure out if file is a directory: */
  163. fh->isdir = is_dir(fh);
  164. } else {
  165. fh->isdir = 1;
  166. strcpy(fh->path, "");
  167. }
  168. return &fh->base;
  169. error:
  170. free(fh);
  171. return NULL;
  172. }
  173. static efi_status_t EFIAPI efi_file_open(struct efi_file_handle *file,
  174. struct efi_file_handle **new_handle,
  175. s16 *file_name, u64 open_mode, u64 attributes)
  176. {
  177. struct file_handle *fh = to_fh(file);
  178. efi_status_t ret;
  179. EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu", file, new_handle, file_name,
  180. open_mode, attributes);
  181. /* Check parameters */
  182. if (!file || !file || !file_name) {
  183. ret = EFI_INVALID_PARAMETER;
  184. goto out;
  185. }
  186. if (open_mode != EFI_FILE_MODE_READ &&
  187. open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE) &&
  188. open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE |
  189. EFI_FILE_MODE_CREATE)) {
  190. ret = EFI_INVALID_PARAMETER;
  191. goto out;
  192. }
  193. /*
  194. * The UEFI spec requires that attributes are only set in create mode.
  195. * The SCT does not care about this and sets EFI_FILE_DIRECTORY in
  196. * read mode. EDK2 does not check that attributes are zero if not in
  197. * create mode.
  198. *
  199. * So here we only check attributes in create mode and do not check
  200. * that they are zero otherwise.
  201. */
  202. if ((open_mode & EFI_FILE_MODE_CREATE) &&
  203. (attributes & (EFI_FILE_READ_ONLY | ~EFI_FILE_VALID_ATTR))) {
  204. ret = EFI_INVALID_PARAMETER;
  205. goto out;
  206. }
  207. /* Open file */
  208. *new_handle = file_open(fh->fs, fh, file_name, open_mode, attributes);
  209. if (*new_handle)
  210. ret = EFI_SUCCESS;
  211. else
  212. ret = EFI_NOT_FOUND;
  213. out:
  214. return EFI_EXIT(ret);
  215. }
  216. static efi_status_t file_close(struct file_handle *fh)
  217. {
  218. fs_closedir(fh->dirs);
  219. free(fh);
  220. return EFI_SUCCESS;
  221. }
  222. static efi_status_t EFIAPI efi_file_close(struct efi_file_handle *file)
  223. {
  224. struct file_handle *fh = to_fh(file);
  225. EFI_ENTRY("%p", file);
  226. return EFI_EXIT(file_close(fh));
  227. }
  228. static efi_status_t EFIAPI efi_file_delete(struct efi_file_handle *file)
  229. {
  230. struct file_handle *fh = to_fh(file);
  231. efi_status_t ret = EFI_SUCCESS;
  232. EFI_ENTRY("%p", file);
  233. if (set_blk_dev(fh)) {
  234. ret = EFI_DEVICE_ERROR;
  235. goto error;
  236. }
  237. if (fs_unlink(fh->path))
  238. ret = EFI_DEVICE_ERROR;
  239. file_close(fh);
  240. error:
  241. return EFI_EXIT(ret);
  242. }
  243. static efi_status_t file_read(struct file_handle *fh, u64 *buffer_size,
  244. void *buffer)
  245. {
  246. loff_t actread;
  247. /* fs_read expects buffer address, not pointer */
  248. uintptr_t buffer_addr = (uintptr_t)map_to_sysmem(buffer);
  249. if (fs_read(fh->path, buffer_addr, fh->offset,
  250. *buffer_size, &actread))
  251. return EFI_DEVICE_ERROR;
  252. *buffer_size = actread;
  253. fh->offset += actread;
  254. return EFI_SUCCESS;
  255. }
  256. static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size,
  257. void *buffer)
  258. {
  259. struct efi_file_info *info = buffer;
  260. struct fs_dirent *dent;
  261. unsigned int required_size;
  262. if (!fh->dirs) {
  263. assert(fh->offset == 0);
  264. fh->dirs = fs_opendir(fh->path);
  265. if (!fh->dirs)
  266. return EFI_DEVICE_ERROR;
  267. }
  268. /*
  269. * So this is a bit awkward. Since fs layer is stateful and we
  270. * can't rewind an entry, in the EFI_BUFFER_TOO_SMALL case below
  271. * we might have to return without consuming the dent.. so we
  272. * have to stash it for next call.
  273. */
  274. if (fh->dent) {
  275. dent = fh->dent;
  276. fh->dent = NULL;
  277. } else {
  278. dent = fs_readdir(fh->dirs);
  279. }
  280. if (!dent) {
  281. /* no more files in directory: */
  282. /* workaround shim.efi bug/quirk.. as find_boot_csv()
  283. * loops through directory contents, it initially calls
  284. * read w/ zero length buffer to find out how much mem
  285. * to allocate for the EFI_FILE_INFO, then allocates,
  286. * and then calls a 2nd time. If we return size of
  287. * zero the first time, it happily passes that to
  288. * AllocateZeroPool(), and when that returns NULL it
  289. * thinks it is EFI_OUT_OF_RESOURCES. So on first
  290. * call return a non-zero size:
  291. */
  292. if (*buffer_size == 0)
  293. *buffer_size = sizeof(*info);
  294. else
  295. *buffer_size = 0;
  296. return EFI_SUCCESS;
  297. }
  298. /* check buffer size: */
  299. required_size = sizeof(*info) + 2 * (strlen(dent->name) + 1);
  300. if (*buffer_size < required_size) {
  301. *buffer_size = required_size;
  302. fh->dent = dent;
  303. return EFI_BUFFER_TOO_SMALL;
  304. }
  305. *buffer_size = required_size;
  306. memset(info, 0, required_size);
  307. info->size = required_size;
  308. info->file_size = dent->size;
  309. info->physical_size = dent->size;
  310. if (dent->type == FS_DT_DIR)
  311. info->attribute |= EFI_FILE_DIRECTORY;
  312. ascii2unicode((u16 *)info->file_name, dent->name);
  313. fh->offset++;
  314. return EFI_SUCCESS;
  315. }
  316. static efi_status_t EFIAPI efi_file_read(struct efi_file_handle *file,
  317. efi_uintn_t *buffer_size, void *buffer)
  318. {
  319. struct file_handle *fh = to_fh(file);
  320. efi_status_t ret = EFI_SUCCESS;
  321. u64 bs;
  322. EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
  323. if (!buffer_size || !buffer) {
  324. ret = EFI_INVALID_PARAMETER;
  325. goto error;
  326. }
  327. if (set_blk_dev(fh)) {
  328. ret = EFI_DEVICE_ERROR;
  329. goto error;
  330. }
  331. bs = *buffer_size;
  332. if (fh->isdir)
  333. ret = dir_read(fh, &bs, buffer);
  334. else
  335. ret = file_read(fh, &bs, buffer);
  336. if (bs <= SIZE_MAX)
  337. *buffer_size = bs;
  338. else
  339. *buffer_size = SIZE_MAX;
  340. error:
  341. return EFI_EXIT(ret);
  342. }
  343. static efi_status_t EFIAPI efi_file_write(struct efi_file_handle *file,
  344. efi_uintn_t *buffer_size,
  345. void *buffer)
  346. {
  347. struct file_handle *fh = to_fh(file);
  348. efi_status_t ret = EFI_SUCCESS;
  349. loff_t actwrite;
  350. EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
  351. if (set_blk_dev(fh)) {
  352. ret = EFI_DEVICE_ERROR;
  353. goto error;
  354. }
  355. if (fs_write(fh->path, (ulong)buffer, fh->offset, *buffer_size,
  356. &actwrite)) {
  357. ret = EFI_DEVICE_ERROR;
  358. goto error;
  359. }
  360. *buffer_size = actwrite;
  361. fh->offset += actwrite;
  362. error:
  363. return EFI_EXIT(ret);
  364. }
  365. static efi_status_t EFIAPI efi_file_getpos(struct efi_file_handle *file,
  366. efi_uintn_t *pos)
  367. {
  368. struct file_handle *fh = to_fh(file);
  369. EFI_ENTRY("%p, %p", file, pos);
  370. if (fh->offset <= SIZE_MAX) {
  371. *pos = fh->offset;
  372. return EFI_EXIT(EFI_SUCCESS);
  373. } else {
  374. return EFI_EXIT(EFI_DEVICE_ERROR);
  375. }
  376. }
  377. static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file,
  378. efi_uintn_t pos)
  379. {
  380. struct file_handle *fh = to_fh(file);
  381. efi_status_t ret = EFI_SUCCESS;
  382. EFI_ENTRY("%p, %zu", file, pos);
  383. if (fh->isdir) {
  384. if (pos != 0) {
  385. ret = EFI_UNSUPPORTED;
  386. goto error;
  387. }
  388. fs_closedir(fh->dirs);
  389. fh->dirs = NULL;
  390. }
  391. if (pos == ~0ULL) {
  392. loff_t file_size;
  393. if (set_blk_dev(fh)) {
  394. ret = EFI_DEVICE_ERROR;
  395. goto error;
  396. }
  397. if (fs_size(fh->path, &file_size)) {
  398. ret = EFI_DEVICE_ERROR;
  399. goto error;
  400. }
  401. pos = file_size;
  402. }
  403. fh->offset = pos;
  404. error:
  405. return EFI_EXIT(ret);
  406. }
  407. static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
  408. const efi_guid_t *info_type,
  409. efi_uintn_t *buffer_size,
  410. void *buffer)
  411. {
  412. struct file_handle *fh = to_fh(file);
  413. efi_status_t ret = EFI_SUCCESS;
  414. EFI_ENTRY("%p, %p, %p, %p", file, info_type, buffer_size, buffer);
  415. if (!guidcmp(info_type, &efi_file_info_guid)) {
  416. struct efi_file_info *info = buffer;
  417. char *filename = basename(fh);
  418. unsigned int required_size;
  419. loff_t file_size;
  420. /* check buffer size: */
  421. required_size = sizeof(*info) + 2 * (strlen(filename) + 1);
  422. if (*buffer_size < required_size) {
  423. *buffer_size = required_size;
  424. ret = EFI_BUFFER_TOO_SMALL;
  425. goto error;
  426. }
  427. if (set_blk_dev(fh)) {
  428. ret = EFI_DEVICE_ERROR;
  429. goto error;
  430. }
  431. if (fs_size(fh->path, &file_size)) {
  432. ret = EFI_DEVICE_ERROR;
  433. goto error;
  434. }
  435. memset(info, 0, required_size);
  436. info->size = required_size;
  437. info->file_size = file_size;
  438. info->physical_size = file_size;
  439. if (fh->isdir)
  440. info->attribute |= EFI_FILE_DIRECTORY;
  441. ascii2unicode((u16 *)info->file_name, filename);
  442. } else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
  443. struct efi_file_system_info *info = buffer;
  444. disk_partition_t part;
  445. efi_uintn_t required_size;
  446. int r;
  447. if (fh->fs->part >= 1)
  448. r = part_get_info(fh->fs->desc, fh->fs->part, &part);
  449. else
  450. r = part_get_info_whole_disk(fh->fs->desc, &part);
  451. if (r < 0) {
  452. ret = EFI_DEVICE_ERROR;
  453. goto error;
  454. }
  455. required_size = sizeof(info) + 2 *
  456. (strlen((const char *)part.name) + 1);
  457. if (*buffer_size < required_size) {
  458. *buffer_size = required_size;
  459. ret = EFI_BUFFER_TOO_SMALL;
  460. goto error;
  461. }
  462. memset(info, 0, required_size);
  463. info->size = required_size;
  464. info->read_only = true;
  465. info->volume_size = part.size * part.blksz;
  466. info->free_space = 0;
  467. info->block_size = part.blksz;
  468. /*
  469. * TODO: The volume label is not available in U-Boot.
  470. * Use the partition name as substitute.
  471. */
  472. ascii2unicode((u16 *)info->volume_label,
  473. (const char *)part.name);
  474. } else {
  475. ret = EFI_UNSUPPORTED;
  476. }
  477. error:
  478. return EFI_EXIT(ret);
  479. }
  480. static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
  481. const efi_guid_t *info_type,
  482. efi_uintn_t buffer_size,
  483. void *buffer)
  484. {
  485. EFI_ENTRY("%p, %p, %zu, %p", file, info_type, buffer_size, buffer);
  486. return EFI_EXIT(EFI_UNSUPPORTED);
  487. }
  488. static efi_status_t EFIAPI efi_file_flush(struct efi_file_handle *file)
  489. {
  490. EFI_ENTRY("%p", file);
  491. return EFI_EXIT(EFI_SUCCESS);
  492. }
  493. static const struct efi_file_handle efi_file_handle_protocol = {
  494. .rev = EFI_FILE_PROTOCOL_REVISION,
  495. .open = efi_file_open,
  496. .close = efi_file_close,
  497. .delete = efi_file_delete,
  498. .read = efi_file_read,
  499. .write = efi_file_write,
  500. .getpos = efi_file_getpos,
  501. .setpos = efi_file_setpos,
  502. .getinfo = efi_file_getinfo,
  503. .setinfo = efi_file_setinfo,
  504. .flush = efi_file_flush,
  505. };
  506. struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp)
  507. {
  508. struct efi_simple_file_system_protocol *v;
  509. struct efi_file_handle *f;
  510. efi_status_t ret;
  511. v = efi_fs_from_path(fp);
  512. if (!v)
  513. return NULL;
  514. EFI_CALL(ret = v->open_volume(v, &f));
  515. if (ret != EFI_SUCCESS)
  516. return NULL;
  517. /* skip over device-path nodes before the file path: */
  518. while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH))
  519. fp = efi_dp_next(fp);
  520. while (fp) {
  521. struct efi_device_path_file_path *fdp =
  522. container_of(fp, struct efi_device_path_file_path, dp);
  523. struct efi_file_handle *f2;
  524. if (!EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) {
  525. printf("bad file path!\n");
  526. f->close(f);
  527. return NULL;
  528. }
  529. EFI_CALL(ret = f->open(f, &f2, (s16 *)fdp->str,
  530. EFI_FILE_MODE_READ, 0));
  531. if (ret != EFI_SUCCESS)
  532. return NULL;
  533. fp = efi_dp_next(fp);
  534. EFI_CALL(f->close(f));
  535. f = f2;
  536. }
  537. return f;
  538. }
  539. static efi_status_t EFIAPI
  540. efi_open_volume(struct efi_simple_file_system_protocol *this,
  541. struct efi_file_handle **root)
  542. {
  543. struct file_system *fs = to_fs(this);
  544. EFI_ENTRY("%p, %p", this, root);
  545. *root = file_open(fs, NULL, NULL, 0, 0);
  546. return EFI_EXIT(EFI_SUCCESS);
  547. }
  548. struct efi_simple_file_system_protocol *
  549. efi_simple_file_system(struct blk_desc *desc, int part,
  550. struct efi_device_path *dp)
  551. {
  552. struct file_system *fs;
  553. fs = calloc(1, sizeof(*fs));
  554. fs->base.rev = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION;
  555. fs->base.open_volume = efi_open_volume;
  556. fs->desc = desc;
  557. fs->part = part;
  558. fs->dp = dp;
  559. return &fs->base;
  560. }