efi_file.c 13 KB

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