efi_file.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * EFI_FILE_PROTOCOL
  4. *
  5. * Copyright (c) 2017 Rob Clark
  6. */
  7. #include <common.h>
  8. #include <charset.h>
  9. #include <efi_loader.h>
  10. #include <log.h>
  11. #include <malloc.h>
  12. #include <mapmem.h>
  13. #include <fs.h>
  14. #include <part.h>
  15. /* GUID for file system information */
  16. const efi_guid_t efi_file_system_info_guid = EFI_FILE_SYSTEM_INFO_GUID;
  17. /* GUID to obtain the volume label */
  18. const efi_guid_t efi_system_volume_label_id = EFI_FILE_SYSTEM_VOLUME_LABEL_ID;
  19. struct file_system {
  20. struct efi_simple_file_system_protocol base;
  21. struct efi_device_path *dp;
  22. struct blk_desc *desc;
  23. int part;
  24. };
  25. #define to_fs(x) container_of(x, struct file_system, base)
  26. struct file_handle {
  27. struct efi_file_handle base;
  28. struct file_system *fs;
  29. loff_t offset; /* current file position/cursor */
  30. int isdir;
  31. u64 open_mode;
  32. /* for reading a directory: */
  33. struct fs_dir_stream *dirs;
  34. struct fs_dirent *dent;
  35. char path[0];
  36. };
  37. #define to_fh(x) container_of(x, struct file_handle, base)
  38. static const struct efi_file_handle efi_file_handle_protocol;
  39. static char *basename(struct file_handle *fh)
  40. {
  41. char *s = strrchr(fh->path, '/');
  42. if (s)
  43. return s + 1;
  44. return fh->path;
  45. }
  46. static int set_blk_dev(struct file_handle *fh)
  47. {
  48. return fs_set_blk_dev_with_part(fh->fs->desc, fh->fs->part);
  49. }
  50. /**
  51. * is_dir() - check if file handle points to directory
  52. *
  53. * We assume that set_blk_dev(fh) has been called already.
  54. *
  55. * @fh: file handle
  56. * Return: true if file handle points to a directory
  57. */
  58. static int is_dir(struct file_handle *fh)
  59. {
  60. struct fs_dir_stream *dirs;
  61. dirs = fs_opendir(fh->path);
  62. if (!dirs)
  63. return 0;
  64. fs_closedir(dirs);
  65. return 1;
  66. }
  67. /*
  68. * Normalize a path which may include either back or fwd slashes,
  69. * double slashes, . or .. entries in the path, etc.
  70. */
  71. static int sanitize_path(char *path)
  72. {
  73. char *p;
  74. /* backslash to slash: */
  75. p = path;
  76. while ((p = strchr(p, '\\')))
  77. *p++ = '/';
  78. /* handle double-slashes: */
  79. p = path;
  80. while ((p = strstr(p, "//"))) {
  81. char *src = p + 1;
  82. memmove(p, src, strlen(src) + 1);
  83. }
  84. /* handle extra /.'s */
  85. p = path;
  86. while ((p = strstr(p, "/."))) {
  87. /*
  88. * You'd be tempted to do this *after* handling ".."s
  89. * below to avoid having to check if "/." is start of
  90. * a "/..", but that won't have the correct results..
  91. * for example, "/foo/./../bar" would get resolved to
  92. * "/foo/bar" if you did these two passes in the other
  93. * order
  94. */
  95. if (p[2] == '.') {
  96. p += 2;
  97. continue;
  98. }
  99. char *src = p + 2;
  100. memmove(p, src, strlen(src) + 1);
  101. }
  102. /* handle extra /..'s: */
  103. p = path;
  104. while ((p = strstr(p, "/.."))) {
  105. char *src = p + 3;
  106. p--;
  107. /* find beginning of previous path entry: */
  108. while (true) {
  109. if (p < path)
  110. return -1;
  111. if (*p == '/')
  112. break;
  113. p--;
  114. }
  115. memmove(p, src, strlen(src) + 1);
  116. }
  117. return 0;
  118. }
  119. /**
  120. * efi_create_file() - create file or directory
  121. *
  122. * @fh: file handle
  123. * @attributes: attributes for newly created file
  124. * Returns: 0 for success
  125. */
  126. static int efi_create_file(struct file_handle *fh, u64 attributes)
  127. {
  128. loff_t actwrite;
  129. void *buffer = &actwrite;
  130. if (attributes & EFI_FILE_DIRECTORY)
  131. return fs_mkdir(fh->path);
  132. else
  133. return fs_write(fh->path, map_to_sysmem(buffer), 0, 0,
  134. &actwrite);
  135. }
  136. /**
  137. * file_open() - open a file handle
  138. *
  139. * @fs: file system
  140. * @parent: directory relative to which the file is to be opened
  141. * @file_name: path of the file to be opened. '\', '.', or '..' may
  142. * be used as modifiers. A leading backslash indicates an
  143. * absolute path.
  144. * @open_mode: bit mask indicating the access mode (read, write,
  145. * create)
  146. * @attributes: attributes for newly created file
  147. * Returns: handle to the opened file or NULL
  148. */
  149. static struct efi_file_handle *file_open(struct file_system *fs,
  150. struct file_handle *parent, u16 *file_name, u64 open_mode,
  151. u64 attributes)
  152. {
  153. struct file_handle *fh;
  154. char f0[MAX_UTF8_PER_UTF16] = {0};
  155. int plen = 0;
  156. int flen = 0;
  157. if (file_name) {
  158. utf16_to_utf8((u8 *)f0, file_name, 1);
  159. flen = u16_strlen(file_name);
  160. }
  161. /* we could have a parent, but also an absolute path: */
  162. if (f0[0] == '\\') {
  163. plen = 0;
  164. } else if (parent) {
  165. plen = strlen(parent->path) + 1;
  166. }
  167. /* +2 is for null and '/' */
  168. fh = calloc(1, sizeof(*fh) + plen + (flen * MAX_UTF8_PER_UTF16) + 2);
  169. fh->open_mode = open_mode;
  170. fh->base = efi_file_handle_protocol;
  171. fh->fs = fs;
  172. if (parent) {
  173. char *p = fh->path;
  174. int exists;
  175. if (plen > 0) {
  176. strcpy(p, parent->path);
  177. p += plen - 1;
  178. *p++ = '/';
  179. }
  180. utf16_to_utf8((u8 *)p, file_name, flen);
  181. if (sanitize_path(fh->path))
  182. goto error;
  183. /* check if file exists: */
  184. if (set_blk_dev(fh))
  185. goto error;
  186. exists = fs_exists(fh->path);
  187. /* fs_exists() calls fs_close(), so open file system again */
  188. if (set_blk_dev(fh))
  189. goto error;
  190. if (!exists) {
  191. if (!(open_mode & EFI_FILE_MODE_CREATE) ||
  192. efi_create_file(fh, attributes))
  193. goto error;
  194. if (set_blk_dev(fh))
  195. goto error;
  196. }
  197. /* figure out if file is a directory: */
  198. fh->isdir = is_dir(fh);
  199. } else {
  200. fh->isdir = 1;
  201. strcpy(fh->path, "");
  202. }
  203. return &fh->base;
  204. error:
  205. free(fh);
  206. return NULL;
  207. }
  208. static efi_status_t EFIAPI efi_file_open(struct efi_file_handle *file,
  209. struct efi_file_handle **new_handle,
  210. u16 *file_name, u64 open_mode, u64 attributes)
  211. {
  212. struct file_handle *fh = to_fh(file);
  213. efi_status_t ret;
  214. EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu", file, new_handle,
  215. file_name, open_mode, attributes);
  216. /* Check parameters */
  217. if (!file || !new_handle || !file_name) {
  218. ret = EFI_INVALID_PARAMETER;
  219. goto out;
  220. }
  221. if (open_mode != EFI_FILE_MODE_READ &&
  222. open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE) &&
  223. open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE |
  224. EFI_FILE_MODE_CREATE)) {
  225. ret = EFI_INVALID_PARAMETER;
  226. goto out;
  227. }
  228. /*
  229. * The UEFI spec requires that attributes are only set in create mode.
  230. * The SCT does not care about this and sets EFI_FILE_DIRECTORY in
  231. * read mode. EDK2 does not check that attributes are zero if not in
  232. * create mode.
  233. *
  234. * So here we only check attributes in create mode and do not check
  235. * that they are zero otherwise.
  236. */
  237. if ((open_mode & EFI_FILE_MODE_CREATE) &&
  238. (attributes & (EFI_FILE_READ_ONLY | ~EFI_FILE_VALID_ATTR))) {
  239. ret = EFI_INVALID_PARAMETER;
  240. goto out;
  241. }
  242. /* Open file */
  243. *new_handle = file_open(fh->fs, fh, file_name, open_mode, attributes);
  244. if (*new_handle) {
  245. EFI_PRINT("file handle %p\n", *new_handle);
  246. ret = EFI_SUCCESS;
  247. } else {
  248. ret = EFI_NOT_FOUND;
  249. }
  250. out:
  251. return EFI_EXIT(ret);
  252. }
  253. static efi_status_t file_close(struct file_handle *fh)
  254. {
  255. fs_closedir(fh->dirs);
  256. free(fh);
  257. return EFI_SUCCESS;
  258. }
  259. static efi_status_t EFIAPI efi_file_close(struct efi_file_handle *file)
  260. {
  261. struct file_handle *fh = to_fh(file);
  262. EFI_ENTRY("%p", file);
  263. return EFI_EXIT(file_close(fh));
  264. }
  265. static efi_status_t EFIAPI efi_file_delete(struct efi_file_handle *file)
  266. {
  267. struct file_handle *fh = to_fh(file);
  268. efi_status_t ret = EFI_SUCCESS;
  269. EFI_ENTRY("%p", file);
  270. if (set_blk_dev(fh) || fs_unlink(fh->path))
  271. ret = EFI_WARN_DELETE_FAILURE;
  272. file_close(fh);
  273. return EFI_EXIT(ret);
  274. }
  275. /**
  276. * efi_get_file_size() - determine the size of a file
  277. *
  278. * @fh: file handle
  279. * @file_size: pointer to receive file size
  280. * Return: status code
  281. */
  282. static efi_status_t efi_get_file_size(struct file_handle *fh,
  283. loff_t *file_size)
  284. {
  285. if (set_blk_dev(fh))
  286. return EFI_DEVICE_ERROR;
  287. if (fs_size(fh->path, file_size))
  288. return EFI_DEVICE_ERROR;
  289. return EFI_SUCCESS;
  290. }
  291. static efi_status_t file_read(struct file_handle *fh, u64 *buffer_size,
  292. void *buffer)
  293. {
  294. loff_t actread;
  295. efi_status_t ret;
  296. loff_t file_size;
  297. if (!buffer) {
  298. ret = EFI_INVALID_PARAMETER;
  299. return ret;
  300. }
  301. ret = efi_get_file_size(fh, &file_size);
  302. if (ret != EFI_SUCCESS)
  303. return ret;
  304. if (file_size < fh->offset) {
  305. ret = EFI_DEVICE_ERROR;
  306. return ret;
  307. }
  308. if (set_blk_dev(fh))
  309. return EFI_DEVICE_ERROR;
  310. if (fs_read(fh->path, map_to_sysmem(buffer), fh->offset,
  311. *buffer_size, &actread))
  312. return EFI_DEVICE_ERROR;
  313. *buffer_size = actread;
  314. fh->offset += actread;
  315. return EFI_SUCCESS;
  316. }
  317. static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size,
  318. void *buffer)
  319. {
  320. struct efi_file_info *info = buffer;
  321. struct fs_dirent *dent;
  322. u64 required_size;
  323. u16 *dst;
  324. if (set_blk_dev(fh))
  325. return EFI_DEVICE_ERROR;
  326. if (!fh->dirs) {
  327. assert(fh->offset == 0);
  328. fh->dirs = fs_opendir(fh->path);
  329. if (!fh->dirs)
  330. return EFI_DEVICE_ERROR;
  331. fh->dent = NULL;
  332. }
  333. /*
  334. * So this is a bit awkward. Since fs layer is stateful and we
  335. * can't rewind an entry, in the EFI_BUFFER_TOO_SMALL case below
  336. * we might have to return without consuming the dent.. so we
  337. * have to stash it for next call.
  338. */
  339. if (fh->dent) {
  340. dent = fh->dent;
  341. } else {
  342. dent = fs_readdir(fh->dirs);
  343. }
  344. if (!dent) {
  345. /* no more files in directory */
  346. *buffer_size = 0;
  347. return EFI_SUCCESS;
  348. }
  349. /* check buffer size: */
  350. required_size = sizeof(*info) +
  351. 2 * (utf8_utf16_strlen(dent->name) + 1);
  352. if (*buffer_size < required_size) {
  353. *buffer_size = required_size;
  354. fh->dent = dent;
  355. return EFI_BUFFER_TOO_SMALL;
  356. }
  357. if (!buffer)
  358. return EFI_INVALID_PARAMETER;
  359. fh->dent = NULL;
  360. *buffer_size = required_size;
  361. memset(info, 0, required_size);
  362. info->size = required_size;
  363. info->file_size = dent->size;
  364. info->physical_size = dent->size;
  365. if (dent->type == FS_DT_DIR)
  366. info->attribute |= EFI_FILE_DIRECTORY;
  367. dst = info->file_name;
  368. utf8_utf16_strcpy(&dst, dent->name);
  369. fh->offset++;
  370. return EFI_SUCCESS;
  371. }
  372. static efi_status_t EFIAPI efi_file_read(struct efi_file_handle *file,
  373. efi_uintn_t *buffer_size, void *buffer)
  374. {
  375. struct file_handle *fh = to_fh(file);
  376. efi_status_t ret = EFI_SUCCESS;
  377. u64 bs;
  378. EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
  379. if (!buffer_size) {
  380. ret = EFI_INVALID_PARAMETER;
  381. goto error;
  382. }
  383. bs = *buffer_size;
  384. if (fh->isdir)
  385. ret = dir_read(fh, &bs, buffer);
  386. else
  387. ret = file_read(fh, &bs, buffer);
  388. if (bs <= SIZE_MAX)
  389. *buffer_size = bs;
  390. else
  391. *buffer_size = SIZE_MAX;
  392. error:
  393. return EFI_EXIT(ret);
  394. }
  395. /**
  396. * efi_file_write() - write to file
  397. *
  398. * This function implements the Write() service of the EFI_FILE_PROTOCOL.
  399. *
  400. * See the Unified Extensible Firmware Interface (UEFI) specification for
  401. * details.
  402. *
  403. * @file: file handle
  404. * @buffer_size: number of bytes to write
  405. * @buffer: buffer with the bytes to write
  406. * Return: status code
  407. */
  408. static efi_status_t EFIAPI efi_file_write(struct efi_file_handle *file,
  409. efi_uintn_t *buffer_size,
  410. void *buffer)
  411. {
  412. struct file_handle *fh = to_fh(file);
  413. efi_status_t ret = EFI_SUCCESS;
  414. loff_t actwrite;
  415. EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer);
  416. if (!file || !buffer_size || !buffer) {
  417. ret = EFI_INVALID_PARAMETER;
  418. goto out;
  419. }
  420. if (fh->isdir) {
  421. ret = EFI_UNSUPPORTED;
  422. goto out;
  423. }
  424. if (!(fh->open_mode & EFI_FILE_MODE_WRITE)) {
  425. ret = EFI_ACCESS_DENIED;
  426. goto out;
  427. }
  428. if (!*buffer_size)
  429. goto out;
  430. if (set_blk_dev(fh)) {
  431. ret = EFI_DEVICE_ERROR;
  432. goto out;
  433. }
  434. if (fs_write(fh->path, map_to_sysmem(buffer), fh->offset, *buffer_size,
  435. &actwrite)) {
  436. ret = EFI_DEVICE_ERROR;
  437. goto out;
  438. }
  439. *buffer_size = actwrite;
  440. fh->offset += actwrite;
  441. out:
  442. return EFI_EXIT(ret);
  443. }
  444. /**
  445. * efi_file_getpos() - get current position in file
  446. *
  447. * This function implements the GetPosition service of the EFI file protocol.
  448. * See the UEFI spec for details.
  449. *
  450. * @file: file handle
  451. * @pos: pointer to file position
  452. * Return: status code
  453. */
  454. static efi_status_t EFIAPI efi_file_getpos(struct efi_file_handle *file,
  455. u64 *pos)
  456. {
  457. efi_status_t ret = EFI_SUCCESS;
  458. struct file_handle *fh = to_fh(file);
  459. EFI_ENTRY("%p, %p", file, pos);
  460. if (fh->isdir) {
  461. ret = EFI_UNSUPPORTED;
  462. goto out;
  463. }
  464. *pos = fh->offset;
  465. out:
  466. return EFI_EXIT(ret);
  467. }
  468. /**
  469. * efi_file_setpos() - set current position in file
  470. *
  471. * This function implements the SetPosition service of the EFI file protocol.
  472. * See the UEFI spec for details.
  473. *
  474. * @file: file handle
  475. * @pos: new file position
  476. * Return: status code
  477. */
  478. static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file,
  479. u64 pos)
  480. {
  481. struct file_handle *fh = to_fh(file);
  482. efi_status_t ret = EFI_SUCCESS;
  483. EFI_ENTRY("%p, %llu", file, pos);
  484. if (fh->isdir) {
  485. if (pos != 0) {
  486. ret = EFI_UNSUPPORTED;
  487. goto error;
  488. }
  489. fs_closedir(fh->dirs);
  490. fh->dirs = NULL;
  491. }
  492. if (pos == ~0ULL) {
  493. loff_t file_size;
  494. ret = efi_get_file_size(fh, &file_size);
  495. if (ret != EFI_SUCCESS)
  496. goto error;
  497. pos = file_size;
  498. }
  499. fh->offset = pos;
  500. error:
  501. return EFI_EXIT(ret);
  502. }
  503. static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
  504. const efi_guid_t *info_type,
  505. efi_uintn_t *buffer_size,
  506. void *buffer)
  507. {
  508. struct file_handle *fh = to_fh(file);
  509. efi_status_t ret = EFI_SUCCESS;
  510. u16 *dst;
  511. EFI_ENTRY("%p, %pUl, %p, %p", file, info_type, buffer_size, buffer);
  512. if (!file || !info_type || !buffer_size ||
  513. (*buffer_size && !buffer)) {
  514. ret = EFI_INVALID_PARAMETER;
  515. goto error;
  516. }
  517. if (!guidcmp(info_type, &efi_file_info_guid)) {
  518. struct efi_file_info *info = buffer;
  519. char *filename = basename(fh);
  520. unsigned int required_size;
  521. loff_t file_size;
  522. /* check buffer size: */
  523. required_size = sizeof(*info) +
  524. 2 * (utf8_utf16_strlen(filename) + 1);
  525. if (*buffer_size < required_size) {
  526. *buffer_size = required_size;
  527. ret = EFI_BUFFER_TOO_SMALL;
  528. goto error;
  529. }
  530. ret = efi_get_file_size(fh, &file_size);
  531. if (ret != EFI_SUCCESS)
  532. goto error;
  533. memset(info, 0, required_size);
  534. info->size = required_size;
  535. info->file_size = file_size;
  536. info->physical_size = file_size;
  537. if (fh->isdir)
  538. info->attribute |= EFI_FILE_DIRECTORY;
  539. dst = info->file_name;
  540. utf8_utf16_strcpy(&dst, filename);
  541. } else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
  542. struct efi_file_system_info *info = buffer;
  543. struct disk_partition part;
  544. efi_uintn_t required_size;
  545. int r;
  546. if (fh->fs->part >= 1)
  547. r = part_get_info(fh->fs->desc, fh->fs->part, &part);
  548. else
  549. r = part_get_info_whole_disk(fh->fs->desc, &part);
  550. if (r < 0) {
  551. ret = EFI_DEVICE_ERROR;
  552. goto error;
  553. }
  554. required_size = sizeof(*info) + 2;
  555. if (*buffer_size < required_size) {
  556. *buffer_size = required_size;
  557. ret = EFI_BUFFER_TOO_SMALL;
  558. goto error;
  559. }
  560. memset(info, 0, required_size);
  561. info->size = required_size;
  562. /*
  563. * TODO: We cannot determine if the volume can be written to.
  564. */
  565. info->read_only = false;
  566. info->volume_size = part.size * part.blksz;
  567. /*
  568. * TODO: We currently have no function to determine the free
  569. * space. The volume size is the best upper bound we have.
  570. */
  571. info->free_space = info->volume_size;
  572. info->block_size = part.blksz;
  573. /*
  574. * TODO: The volume label is not available in U-Boot.
  575. */
  576. info->volume_label[0] = 0;
  577. } else if (!guidcmp(info_type, &efi_system_volume_label_id)) {
  578. if (*buffer_size < 2) {
  579. *buffer_size = 2;
  580. ret = EFI_BUFFER_TOO_SMALL;
  581. goto error;
  582. }
  583. *(u16 *)buffer = 0;
  584. } else {
  585. ret = EFI_UNSUPPORTED;
  586. }
  587. error:
  588. return EFI_EXIT(ret);
  589. }
  590. static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
  591. const efi_guid_t *info_type,
  592. efi_uintn_t buffer_size,
  593. void *buffer)
  594. {
  595. struct file_handle *fh = to_fh(file);
  596. efi_status_t ret = EFI_UNSUPPORTED;
  597. EFI_ENTRY("%p, %pUl, %zu, %p", file, info_type, buffer_size, buffer);
  598. if (!guidcmp(info_type, &efi_file_info_guid)) {
  599. struct efi_file_info *info = (struct efi_file_info *)buffer;
  600. char *filename = basename(fh);
  601. char *new_file_name, *pos;
  602. loff_t file_size;
  603. /* The buffer will always contain a file name. */
  604. if (buffer_size < sizeof(struct efi_file_info) + 2 ||
  605. buffer_size < info->size) {
  606. ret = EFI_BAD_BUFFER_SIZE;
  607. goto out;
  608. }
  609. /* We cannot change the directory attribute */
  610. if (!fh->isdir != !(info->attribute & EFI_FILE_DIRECTORY)) {
  611. ret = EFI_ACCESS_DENIED;
  612. goto out;
  613. }
  614. /* Check for renaming */
  615. new_file_name = malloc(utf16_utf8_strlen(info->file_name));
  616. if (!new_file_name) {
  617. ret = EFI_OUT_OF_RESOURCES;
  618. goto out;
  619. }
  620. pos = new_file_name;
  621. utf16_utf8_strcpy(&pos, info->file_name);
  622. if (strcmp(new_file_name, filename)) {
  623. /* TODO: we do not support renaming */
  624. EFI_PRINT("Renaming not supported\n");
  625. free(new_file_name);
  626. ret = EFI_ACCESS_DENIED;
  627. goto out;
  628. }
  629. free(new_file_name);
  630. /* Check for truncation */
  631. ret = efi_get_file_size(fh, &file_size);
  632. if (ret != EFI_SUCCESS)
  633. goto out;
  634. if (file_size != info->file_size) {
  635. /* TODO: we do not support truncation */
  636. EFI_PRINT("Truncation not supported\n");
  637. ret = EFI_ACCESS_DENIED;
  638. goto out;
  639. }
  640. /*
  641. * We do not care for the other attributes
  642. * TODO: Support read only
  643. */
  644. ret = EFI_SUCCESS;
  645. } else {
  646. /* TODO: We do not support changing the volume label */
  647. ret = EFI_UNSUPPORTED;
  648. }
  649. out:
  650. return EFI_EXIT(ret);
  651. }
  652. static efi_status_t EFIAPI efi_file_flush(struct efi_file_handle *file)
  653. {
  654. EFI_ENTRY("%p", file);
  655. return EFI_EXIT(EFI_SUCCESS);
  656. }
  657. static efi_status_t EFIAPI efi_file_open_ex(struct efi_file_handle *file,
  658. struct efi_file_handle **new_handle,
  659. u16 *file_name, u64 open_mode, u64 attributes,
  660. struct efi_file_io_token *token)
  661. {
  662. return EFI_UNSUPPORTED;
  663. }
  664. static efi_status_t EFIAPI efi_file_read_ex(struct efi_file_handle *file,
  665. struct efi_file_io_token *token)
  666. {
  667. return EFI_UNSUPPORTED;
  668. }
  669. static efi_status_t EFIAPI efi_file_write_ex(struct efi_file_handle *file,
  670. struct efi_file_io_token *token)
  671. {
  672. return EFI_UNSUPPORTED;
  673. }
  674. static efi_status_t EFIAPI efi_file_flush_ex(struct efi_file_handle *file,
  675. struct efi_file_io_token *token)
  676. {
  677. return EFI_UNSUPPORTED;
  678. }
  679. static const struct efi_file_handle efi_file_handle_protocol = {
  680. .rev = EFI_FILE_PROTOCOL_REVISION2,
  681. .open = efi_file_open,
  682. .close = efi_file_close,
  683. .delete = efi_file_delete,
  684. .read = efi_file_read,
  685. .write = efi_file_write,
  686. .getpos = efi_file_getpos,
  687. .setpos = efi_file_setpos,
  688. .getinfo = efi_file_getinfo,
  689. .setinfo = efi_file_setinfo,
  690. .flush = efi_file_flush,
  691. .open_ex = efi_file_open_ex,
  692. .read_ex = efi_file_read_ex,
  693. .write_ex = efi_file_write_ex,
  694. .flush_ex = efi_file_flush_ex,
  695. };
  696. /**
  697. * efi_file_from_path() - open file via device path
  698. *
  699. * @fp: device path
  700. * @return: EFI_FILE_PROTOCOL for the file or NULL
  701. */
  702. struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp)
  703. {
  704. struct efi_simple_file_system_protocol *v;
  705. struct efi_file_handle *f;
  706. efi_status_t ret;
  707. v = efi_fs_from_path(fp);
  708. if (!v)
  709. return NULL;
  710. EFI_CALL(ret = v->open_volume(v, &f));
  711. if (ret != EFI_SUCCESS)
  712. return NULL;
  713. /* Skip over device-path nodes before the file path. */
  714. while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH))
  715. fp = efi_dp_next(fp);
  716. /*
  717. * Step through the nodes of the directory path until the actual file
  718. * node is reached which is the final node in the device path.
  719. */
  720. while (fp) {
  721. struct efi_device_path_file_path *fdp =
  722. container_of(fp, struct efi_device_path_file_path, dp);
  723. struct efi_file_handle *f2;
  724. u16 *filename;
  725. if (!EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) {
  726. printf("bad file path!\n");
  727. f->close(f);
  728. return NULL;
  729. }
  730. filename = u16_strdup(fdp->str);
  731. if (!filename)
  732. return NULL;
  733. EFI_CALL(ret = f->open(f, &f2, filename,
  734. EFI_FILE_MODE_READ, 0));
  735. free(filename);
  736. if (ret != EFI_SUCCESS)
  737. return NULL;
  738. fp = efi_dp_next(fp);
  739. EFI_CALL(f->close(f));
  740. f = f2;
  741. }
  742. return f;
  743. }
  744. static efi_status_t EFIAPI
  745. efi_open_volume(struct efi_simple_file_system_protocol *this,
  746. struct efi_file_handle **root)
  747. {
  748. struct file_system *fs = to_fs(this);
  749. EFI_ENTRY("%p, %p", this, root);
  750. *root = file_open(fs, NULL, NULL, 0, 0);
  751. return EFI_EXIT(EFI_SUCCESS);
  752. }
  753. struct efi_simple_file_system_protocol *
  754. efi_simple_file_system(struct blk_desc *desc, int part,
  755. struct efi_device_path *dp)
  756. {
  757. struct file_system *fs;
  758. fs = calloc(1, sizeof(*fs));
  759. fs->base.rev = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION;
  760. fs->base.open_volume = efi_open_volume;
  761. fs->desc = desc;
  762. fs->part = part;
  763. fs->dp = dp;
  764. return &fs->base;
  765. }