efi_file.c 20 KB

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