efi_file.c 13 KB

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