efi_file.c 15 KB

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