efi_file.c 12 KB

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