efi_file.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  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 || !new_handle || !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. if (fs_read(fh->path, map_to_sysmem(buffer), fh->offset,
  248. *buffer_size, &actread))
  249. return EFI_DEVICE_ERROR;
  250. *buffer_size = actread;
  251. fh->offset += actread;
  252. return EFI_SUCCESS;
  253. }
  254. static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size,
  255. void *buffer)
  256. {
  257. struct efi_file_info *info = buffer;
  258. struct fs_dirent *dent;
  259. unsigned int required_size;
  260. if (!fh->dirs) {
  261. assert(fh->offset == 0);
  262. fh->dirs = fs_opendir(fh->path);
  263. if (!fh->dirs)
  264. return EFI_DEVICE_ERROR;
  265. }
  266. /*
  267. * So this is a bit awkward. Since fs layer is stateful and we
  268. * can't rewind an entry, in the EFI_BUFFER_TOO_SMALL case below
  269. * we might have to return without consuming the dent.. so we
  270. * have to stash it for next call.
  271. */
  272. if (fh->dent) {
  273. dent = fh->dent;
  274. fh->dent = NULL;
  275. } else {
  276. dent = fs_readdir(fh->dirs);
  277. }
  278. if (!dent) {
  279. /* no more files in directory: */
  280. /* workaround shim.efi bug/quirk.. as find_boot_csv()
  281. * loops through directory contents, it initially calls
  282. * read w/ zero length buffer to find out how much mem
  283. * to allocate for the EFI_FILE_INFO, then allocates,
  284. * and then calls a 2nd time. If we return size of
  285. * zero the first time, it happily passes that to
  286. * AllocateZeroPool(), and when that returns NULL it
  287. * thinks it is EFI_OUT_OF_RESOURCES. So on first
  288. * call return a non-zero size:
  289. */
  290. if (*buffer_size == 0)
  291. *buffer_size = sizeof(*info);
  292. else
  293. *buffer_size = 0;
  294. return EFI_SUCCESS;
  295. }
  296. /* check buffer size: */
  297. required_size = sizeof(*info) + 2 * (strlen(dent->name) + 1);
  298. if (*buffer_size < required_size) {
  299. *buffer_size = required_size;
  300. fh->dent = dent;
  301. return EFI_BUFFER_TOO_SMALL;
  302. }
  303. *buffer_size = required_size;
  304. memset(info, 0, required_size);
  305. info->size = required_size;
  306. info->file_size = dent->size;
  307. info->physical_size = dent->size;
  308. if (dent->type == FS_DT_DIR)
  309. info->attribute |= EFI_FILE_DIRECTORY;
  310. ascii2unicode((u16 *)info->file_name, dent->name);
  311. fh->offset++;
  312. return EFI_SUCCESS;
  313. }
  314. static efi_status_t EFIAPI efi_file_read(struct efi_file_handle *file,
  315. efi_uintn_t *buffer_size, void *buffer)
  316. {
  317. struct file_handle *fh = to_fh(file);
  318. efi_status_t ret = EFI_SUCCESS;
  319. u64 bs;
  320. EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
  321. if (!buffer_size || !buffer) {
  322. ret = EFI_INVALID_PARAMETER;
  323. goto error;
  324. }
  325. if (set_blk_dev(fh)) {
  326. ret = EFI_DEVICE_ERROR;
  327. goto error;
  328. }
  329. bs = *buffer_size;
  330. if (fh->isdir)
  331. ret = dir_read(fh, &bs, buffer);
  332. else
  333. ret = file_read(fh, &bs, buffer);
  334. if (bs <= SIZE_MAX)
  335. *buffer_size = bs;
  336. else
  337. *buffer_size = SIZE_MAX;
  338. error:
  339. return EFI_EXIT(ret);
  340. }
  341. static efi_status_t EFIAPI efi_file_write(struct efi_file_handle *file,
  342. efi_uintn_t *buffer_size,
  343. void *buffer)
  344. {
  345. struct file_handle *fh = to_fh(file);
  346. efi_status_t ret = EFI_SUCCESS;
  347. loff_t actwrite;
  348. EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
  349. if (set_blk_dev(fh)) {
  350. ret = EFI_DEVICE_ERROR;
  351. goto error;
  352. }
  353. if (fs_write(fh->path, map_to_sysmem(buffer), fh->offset, *buffer_size,
  354. &actwrite)) {
  355. ret = EFI_DEVICE_ERROR;
  356. goto error;
  357. }
  358. *buffer_size = actwrite;
  359. fh->offset += actwrite;
  360. error:
  361. return EFI_EXIT(ret);
  362. }
  363. static efi_status_t EFIAPI efi_file_getpos(struct efi_file_handle *file,
  364. efi_uintn_t *pos)
  365. {
  366. struct file_handle *fh = to_fh(file);
  367. EFI_ENTRY("%p, %p", file, pos);
  368. if (fh->offset <= SIZE_MAX) {
  369. *pos = fh->offset;
  370. return EFI_EXIT(EFI_SUCCESS);
  371. } else {
  372. return EFI_EXIT(EFI_DEVICE_ERROR);
  373. }
  374. }
  375. static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file,
  376. efi_uintn_t pos)
  377. {
  378. struct file_handle *fh = to_fh(file);
  379. efi_status_t ret = EFI_SUCCESS;
  380. EFI_ENTRY("%p, %zu", file, pos);
  381. if (fh->isdir) {
  382. if (pos != 0) {
  383. ret = EFI_UNSUPPORTED;
  384. goto error;
  385. }
  386. fs_closedir(fh->dirs);
  387. fh->dirs = NULL;
  388. }
  389. if (pos == ~0ULL) {
  390. loff_t file_size;
  391. if (set_blk_dev(fh)) {
  392. ret = EFI_DEVICE_ERROR;
  393. goto error;
  394. }
  395. if (fs_size(fh->path, &file_size)) {
  396. ret = EFI_DEVICE_ERROR;
  397. goto error;
  398. }
  399. pos = file_size;
  400. }
  401. fh->offset = pos;
  402. error:
  403. return EFI_EXIT(ret);
  404. }
  405. static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
  406. const efi_guid_t *info_type,
  407. efi_uintn_t *buffer_size,
  408. void *buffer)
  409. {
  410. struct file_handle *fh = to_fh(file);
  411. efi_status_t ret = EFI_SUCCESS;
  412. EFI_ENTRY("%p, %pUl, %p, %p", file, info_type, buffer_size, buffer);
  413. if (!guidcmp(info_type, &efi_file_info_guid)) {
  414. struct efi_file_info *info = buffer;
  415. char *filename = basename(fh);
  416. unsigned int required_size;
  417. loff_t file_size;
  418. /* check buffer size: */
  419. required_size = sizeof(*info) + 2 * (strlen(filename) + 1);
  420. if (*buffer_size < required_size) {
  421. *buffer_size = required_size;
  422. ret = EFI_BUFFER_TOO_SMALL;
  423. goto error;
  424. }
  425. if (set_blk_dev(fh)) {
  426. ret = EFI_DEVICE_ERROR;
  427. goto error;
  428. }
  429. if (fs_size(fh->path, &file_size)) {
  430. ret = EFI_DEVICE_ERROR;
  431. goto error;
  432. }
  433. memset(info, 0, required_size);
  434. info->size = required_size;
  435. info->file_size = file_size;
  436. info->physical_size = file_size;
  437. if (fh->isdir)
  438. info->attribute |= EFI_FILE_DIRECTORY;
  439. ascii2unicode((u16 *)info->file_name, filename);
  440. } else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
  441. struct efi_file_system_info *info = buffer;
  442. disk_partition_t part;
  443. efi_uintn_t required_size;
  444. int r;
  445. if (fh->fs->part >= 1)
  446. r = part_get_info(fh->fs->desc, fh->fs->part, &part);
  447. else
  448. r = part_get_info_whole_disk(fh->fs->desc, &part);
  449. if (r < 0) {
  450. ret = EFI_DEVICE_ERROR;
  451. goto error;
  452. }
  453. required_size = sizeof(info) + 2 *
  454. (strlen((const char *)part.name) + 1);
  455. if (*buffer_size < required_size) {
  456. *buffer_size = required_size;
  457. ret = EFI_BUFFER_TOO_SMALL;
  458. goto error;
  459. }
  460. memset(info, 0, required_size);
  461. info->size = required_size;
  462. info->read_only = true;
  463. info->volume_size = part.size * part.blksz;
  464. info->free_space = 0;
  465. info->block_size = part.blksz;
  466. /*
  467. * TODO: The volume label is not available in U-Boot.
  468. * Use the partition name as substitute.
  469. */
  470. ascii2unicode((u16 *)info->volume_label,
  471. (const char *)part.name);
  472. } else {
  473. ret = EFI_UNSUPPORTED;
  474. }
  475. error:
  476. return EFI_EXIT(ret);
  477. }
  478. static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
  479. const efi_guid_t *info_type,
  480. efi_uintn_t buffer_size,
  481. void *buffer)
  482. {
  483. EFI_ENTRY("%p, %p, %zu, %p", file, info_type, buffer_size, buffer);
  484. return EFI_EXIT(EFI_UNSUPPORTED);
  485. }
  486. static efi_status_t EFIAPI efi_file_flush(struct efi_file_handle *file)
  487. {
  488. EFI_ENTRY("%p", file);
  489. return EFI_EXIT(EFI_SUCCESS);
  490. }
  491. static const struct efi_file_handle efi_file_handle_protocol = {
  492. .rev = EFI_FILE_PROTOCOL_REVISION,
  493. .open = efi_file_open,
  494. .close = efi_file_close,
  495. .delete = efi_file_delete,
  496. .read = efi_file_read,
  497. .write = efi_file_write,
  498. .getpos = efi_file_getpos,
  499. .setpos = efi_file_setpos,
  500. .getinfo = efi_file_getinfo,
  501. .setinfo = efi_file_setinfo,
  502. .flush = efi_file_flush,
  503. };
  504. struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp)
  505. {
  506. struct efi_simple_file_system_protocol *v;
  507. struct efi_file_handle *f;
  508. efi_status_t ret;
  509. v = efi_fs_from_path(fp);
  510. if (!v)
  511. return NULL;
  512. EFI_CALL(ret = v->open_volume(v, &f));
  513. if (ret != EFI_SUCCESS)
  514. return NULL;
  515. /* skip over device-path nodes before the file path: */
  516. while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH))
  517. fp = efi_dp_next(fp);
  518. while (fp) {
  519. struct efi_device_path_file_path *fdp =
  520. container_of(fp, struct efi_device_path_file_path, dp);
  521. struct efi_file_handle *f2;
  522. if (!EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) {
  523. printf("bad file path!\n");
  524. f->close(f);
  525. return NULL;
  526. }
  527. EFI_CALL(ret = f->open(f, &f2, (s16 *)fdp->str,
  528. EFI_FILE_MODE_READ, 0));
  529. if (ret != EFI_SUCCESS)
  530. return NULL;
  531. fp = efi_dp_next(fp);
  532. EFI_CALL(f->close(f));
  533. f = f2;
  534. }
  535. return f;
  536. }
  537. static efi_status_t EFIAPI
  538. efi_open_volume(struct efi_simple_file_system_protocol *this,
  539. struct efi_file_handle **root)
  540. {
  541. struct file_system *fs = to_fs(this);
  542. EFI_ENTRY("%p, %p", this, root);
  543. *root = file_open(fs, NULL, NULL, 0, 0);
  544. return EFI_EXIT(EFI_SUCCESS);
  545. }
  546. struct efi_simple_file_system_protocol *
  547. efi_simple_file_system(struct blk_desc *desc, int part,
  548. struct efi_device_path *dp)
  549. {
  550. struct file_system *fs;
  551. fs = calloc(1, sizeof(*fs));
  552. fs->base.rev = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION;
  553. fs->base.open_volume = efi_open_volume;
  554. fs->desc = desc;
  555. fs->part = part;
  556. fs->dp = dp;
  557. return &fs->base;
  558. }