efi_file.c 13 KB

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