efi_file.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  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 efi_file_open_int(struct efi_file_handle *this,
  209. struct efi_file_handle **new_handle,
  210. u16 *file_name, u64 open_mode,
  211. u64 attributes)
  212. {
  213. struct file_handle *fh = to_fh(this);
  214. efi_status_t ret;
  215. /* Check parameters */
  216. if (!this || !new_handle || !file_name) {
  217. ret = EFI_INVALID_PARAMETER;
  218. goto out;
  219. }
  220. if (open_mode != EFI_FILE_MODE_READ &&
  221. open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE) &&
  222. open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE |
  223. EFI_FILE_MODE_CREATE)) {
  224. ret = EFI_INVALID_PARAMETER;
  225. goto out;
  226. }
  227. /*
  228. * The UEFI spec requires that attributes are only set in create mode.
  229. * The SCT does not care about this and sets EFI_FILE_DIRECTORY in
  230. * read mode. EDK2 does not check that attributes are zero if not in
  231. * create mode.
  232. *
  233. * So here we only check attributes in create mode and do not check
  234. * that they are zero otherwise.
  235. */
  236. if ((open_mode & EFI_FILE_MODE_CREATE) &&
  237. (attributes & (EFI_FILE_READ_ONLY | ~EFI_FILE_VALID_ATTR))) {
  238. ret = EFI_INVALID_PARAMETER;
  239. goto out;
  240. }
  241. /* Open file */
  242. *new_handle = file_open(fh->fs, fh, file_name, open_mode, attributes);
  243. if (*new_handle) {
  244. EFI_PRINT("file handle %p\n", *new_handle);
  245. ret = EFI_SUCCESS;
  246. } else {
  247. ret = EFI_NOT_FOUND;
  248. }
  249. out:
  250. return ret;
  251. }
  252. /**
  253. * efi_file_open_()
  254. *
  255. * This function implements the Open service of the File Protocol.
  256. * See the UEFI spec for details.
  257. *
  258. * @this: EFI_FILE_PROTOCOL instance
  259. * @new_handle: on return pointer to file handle
  260. * @file_name: file name
  261. * @open_mode: mode to open the file (read, read/write, create/read/write)
  262. * @attributes: attributes for newly created file
  263. */
  264. static efi_status_t EFIAPI efi_file_open(struct efi_file_handle *this,
  265. struct efi_file_handle **new_handle,
  266. u16 *file_name, u64 open_mode,
  267. u64 attributes)
  268. {
  269. efi_status_t ret;
  270. EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu", this, new_handle,
  271. file_name, open_mode, attributes);
  272. ret = efi_file_open_int(this, new_handle, file_name, open_mode,
  273. attributes);
  274. return EFI_EXIT(ret);
  275. }
  276. /**
  277. * efi_file_open_ex() - open file asynchronously
  278. *
  279. * This function implements the OpenEx service of the File Protocol.
  280. * See the UEFI spec for details.
  281. *
  282. * @this: EFI_FILE_PROTOCOL instance
  283. * @new_handle: on return pointer to file handle
  284. * @file_name: file name
  285. * @open_mode: mode to open the file (read, read/write, create/read/write)
  286. * @attributes: attributes for newly created file
  287. * @token: transaction token
  288. */
  289. static efi_status_t EFIAPI efi_file_open_ex(struct efi_file_handle *this,
  290. struct efi_file_handle **new_handle,
  291. u16 *file_name, u64 open_mode,
  292. u64 attributes,
  293. struct efi_file_io_token *token)
  294. {
  295. efi_status_t ret;
  296. EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu, %p", this, new_handle,
  297. file_name, open_mode, attributes, token);
  298. if (!token) {
  299. ret = EFI_INVALID_PARAMETER;
  300. goto out;
  301. }
  302. ret = efi_file_open_int(this, new_handle, file_name, open_mode,
  303. attributes);
  304. if (ret == EFI_SUCCESS && token->event) {
  305. token->status = EFI_SUCCESS;
  306. efi_signal_event(token->event);
  307. }
  308. out:
  309. return EFI_EXIT(ret);
  310. }
  311. static efi_status_t file_close(struct file_handle *fh)
  312. {
  313. fs_closedir(fh->dirs);
  314. free(fh);
  315. return EFI_SUCCESS;
  316. }
  317. static efi_status_t EFIAPI efi_file_close(struct efi_file_handle *file)
  318. {
  319. struct file_handle *fh = to_fh(file);
  320. EFI_ENTRY("%p", file);
  321. return EFI_EXIT(file_close(fh));
  322. }
  323. static efi_status_t EFIAPI efi_file_delete(struct efi_file_handle *file)
  324. {
  325. struct file_handle *fh = to_fh(file);
  326. efi_status_t ret = EFI_SUCCESS;
  327. EFI_ENTRY("%p", file);
  328. if (set_blk_dev(fh) || fs_unlink(fh->path))
  329. ret = EFI_WARN_DELETE_FAILURE;
  330. file_close(fh);
  331. return EFI_EXIT(ret);
  332. }
  333. /**
  334. * efi_get_file_size() - determine the size of a file
  335. *
  336. * @fh: file handle
  337. * @file_size: pointer to receive file size
  338. * Return: status code
  339. */
  340. static efi_status_t efi_get_file_size(struct file_handle *fh,
  341. loff_t *file_size)
  342. {
  343. if (set_blk_dev(fh))
  344. return EFI_DEVICE_ERROR;
  345. if (fs_size(fh->path, file_size))
  346. return EFI_DEVICE_ERROR;
  347. return EFI_SUCCESS;
  348. }
  349. static efi_status_t file_read(struct file_handle *fh, u64 *buffer_size,
  350. void *buffer)
  351. {
  352. loff_t actread;
  353. efi_status_t ret;
  354. loff_t file_size;
  355. if (!buffer) {
  356. ret = EFI_INVALID_PARAMETER;
  357. return ret;
  358. }
  359. ret = efi_get_file_size(fh, &file_size);
  360. if (ret != EFI_SUCCESS)
  361. return ret;
  362. if (file_size < fh->offset) {
  363. ret = EFI_DEVICE_ERROR;
  364. return ret;
  365. }
  366. if (set_blk_dev(fh))
  367. return EFI_DEVICE_ERROR;
  368. if (fs_read(fh->path, map_to_sysmem(buffer), fh->offset,
  369. *buffer_size, &actread))
  370. return EFI_DEVICE_ERROR;
  371. *buffer_size = actread;
  372. fh->offset += actread;
  373. return EFI_SUCCESS;
  374. }
  375. static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size,
  376. void *buffer)
  377. {
  378. struct efi_file_info *info = buffer;
  379. struct fs_dirent *dent;
  380. u64 required_size;
  381. u16 *dst;
  382. if (set_blk_dev(fh))
  383. return EFI_DEVICE_ERROR;
  384. if (!fh->dirs) {
  385. assert(fh->offset == 0);
  386. fh->dirs = fs_opendir(fh->path);
  387. if (!fh->dirs)
  388. return EFI_DEVICE_ERROR;
  389. fh->dent = NULL;
  390. }
  391. /*
  392. * So this is a bit awkward. Since fs layer is stateful and we
  393. * can't rewind an entry, in the EFI_BUFFER_TOO_SMALL case below
  394. * we might have to return without consuming the dent.. so we
  395. * have to stash it for next call.
  396. */
  397. if (fh->dent) {
  398. dent = fh->dent;
  399. } else {
  400. dent = fs_readdir(fh->dirs);
  401. }
  402. if (!dent) {
  403. /* no more files in directory */
  404. *buffer_size = 0;
  405. return EFI_SUCCESS;
  406. }
  407. /* check buffer size: */
  408. required_size = sizeof(*info) +
  409. 2 * (utf8_utf16_strlen(dent->name) + 1);
  410. if (*buffer_size < required_size) {
  411. *buffer_size = required_size;
  412. fh->dent = dent;
  413. return EFI_BUFFER_TOO_SMALL;
  414. }
  415. if (!buffer)
  416. return EFI_INVALID_PARAMETER;
  417. fh->dent = NULL;
  418. *buffer_size = required_size;
  419. memset(info, 0, required_size);
  420. info->size = required_size;
  421. info->file_size = dent->size;
  422. info->physical_size = dent->size;
  423. if (dent->type == FS_DT_DIR)
  424. info->attribute |= EFI_FILE_DIRECTORY;
  425. dst = info->file_name;
  426. utf8_utf16_strcpy(&dst, dent->name);
  427. fh->offset++;
  428. return EFI_SUCCESS;
  429. }
  430. static efi_status_t efi_file_read_int(struct efi_file_handle *this,
  431. efi_uintn_t *buffer_size, void *buffer)
  432. {
  433. struct file_handle *fh = to_fh(this);
  434. efi_status_t ret = EFI_SUCCESS;
  435. u64 bs;
  436. if (!this || !buffer_size || !buffer)
  437. return EFI_INVALID_PARAMETER;
  438. bs = *buffer_size;
  439. if (fh->isdir)
  440. ret = dir_read(fh, &bs, buffer);
  441. else
  442. ret = file_read(fh, &bs, buffer);
  443. if (bs <= SIZE_MAX)
  444. *buffer_size = bs;
  445. else
  446. *buffer_size = SIZE_MAX;
  447. return ret;
  448. }
  449. /**
  450. * efi_file_read() - read file
  451. *
  452. * This function implements the Read() service of the EFI_FILE_PROTOCOL.
  453. *
  454. * See the Unified Extensible Firmware Interface (UEFI) specification for
  455. * details.
  456. *
  457. * @this: file protocol instance
  458. * @buffer_size: number of bytes to read
  459. * @buffer: read buffer
  460. * Return: status code
  461. */
  462. static efi_status_t EFIAPI efi_file_read(struct efi_file_handle *this,
  463. efi_uintn_t *buffer_size, void *buffer)
  464. {
  465. efi_status_t ret;
  466. EFI_ENTRY("%p, %p, %p", this, buffer_size, buffer);
  467. ret = efi_file_read_int(this, buffer_size, buffer);
  468. return EFI_EXIT(ret);
  469. }
  470. /**
  471. * efi_file_read_ex() - read file asynchonously
  472. *
  473. * This function implements the ReadEx() service of the EFI_FILE_PROTOCOL.
  474. *
  475. * See the Unified Extensible Firmware Interface (UEFI) specification for
  476. * details.
  477. *
  478. * @this: file protocol instance
  479. * @token: transaction token
  480. * Return: status code
  481. */
  482. static efi_status_t EFIAPI efi_file_read_ex(struct efi_file_handle *this,
  483. struct efi_file_io_token *token)
  484. {
  485. efi_status_t ret;
  486. EFI_ENTRY("%p, %p", this, token);
  487. if (!token) {
  488. ret = EFI_INVALID_PARAMETER;
  489. goto out;
  490. }
  491. ret = efi_file_read_int(this, &token->buffer_size, token->buffer);
  492. if (ret == EFI_SUCCESS && token->event) {
  493. token->status = EFI_SUCCESS;
  494. efi_signal_event(token->event);
  495. }
  496. out:
  497. return EFI_EXIT(ret);
  498. }
  499. static efi_status_t efi_file_write_int(struct efi_file_handle *this,
  500. efi_uintn_t *buffer_size, void *buffer)
  501. {
  502. struct file_handle *fh = to_fh(this);
  503. efi_status_t ret = EFI_SUCCESS;
  504. loff_t actwrite;
  505. if (!this || !buffer_size || !buffer) {
  506. ret = EFI_INVALID_PARAMETER;
  507. goto out;
  508. }
  509. if (fh->isdir) {
  510. ret = EFI_UNSUPPORTED;
  511. goto out;
  512. }
  513. if (!(fh->open_mode & EFI_FILE_MODE_WRITE)) {
  514. ret = EFI_ACCESS_DENIED;
  515. goto out;
  516. }
  517. if (!*buffer_size)
  518. goto out;
  519. if (set_blk_dev(fh)) {
  520. ret = EFI_DEVICE_ERROR;
  521. goto out;
  522. }
  523. if (fs_write(fh->path, map_to_sysmem(buffer), fh->offset, *buffer_size,
  524. &actwrite)) {
  525. ret = EFI_DEVICE_ERROR;
  526. goto out;
  527. }
  528. *buffer_size = actwrite;
  529. fh->offset += actwrite;
  530. out:
  531. return ret;
  532. }
  533. /**
  534. * efi_file_write() - write to file
  535. *
  536. * This function implements the Write() service of the EFI_FILE_PROTOCOL.
  537. *
  538. * See the Unified Extensible Firmware Interface (UEFI) specification for
  539. * details.
  540. *
  541. * @this: file protocol instance
  542. * @buffer_size: number of bytes to write
  543. * @buffer: buffer with the bytes to write
  544. * Return: status code
  545. */
  546. static efi_status_t EFIAPI efi_file_write(struct efi_file_handle *this,
  547. efi_uintn_t *buffer_size,
  548. void *buffer)
  549. {
  550. efi_status_t ret;
  551. EFI_ENTRY("%p, %p, %p", this, buffer_size, buffer);
  552. ret = efi_file_write_int(this, buffer_size, buffer);
  553. return EFI_EXIT(ret);
  554. }
  555. /**
  556. * efi_file_write_ex() - write to file
  557. *
  558. * This function implements the WriteEx() service of the EFI_FILE_PROTOCOL.
  559. *
  560. * See the Unified Extensible Firmware Interface (UEFI) specification for
  561. * details.
  562. *
  563. * @this: file protocol instance
  564. * @token: transaction token
  565. * Return: status code
  566. */
  567. static efi_status_t EFIAPI efi_file_write_ex(struct efi_file_handle *this,
  568. struct efi_file_io_token *token)
  569. {
  570. efi_status_t ret;
  571. EFI_ENTRY("%p, %p", this, token);
  572. if (!token) {
  573. ret = EFI_INVALID_PARAMETER;
  574. goto out;
  575. }
  576. ret = efi_file_write_int(this, &token->buffer_size, token->buffer);
  577. if (ret == EFI_SUCCESS && token->event) {
  578. token->status = EFI_SUCCESS;
  579. efi_signal_event(token->event);
  580. }
  581. out:
  582. return EFI_EXIT(ret);
  583. }
  584. /**
  585. * efi_file_getpos() - get current position in file
  586. *
  587. * This function implements the GetPosition service of the EFI file protocol.
  588. * See the UEFI spec for details.
  589. *
  590. * @file: file handle
  591. * @pos: pointer to file position
  592. * Return: status code
  593. */
  594. static efi_status_t EFIAPI efi_file_getpos(struct efi_file_handle *file,
  595. u64 *pos)
  596. {
  597. efi_status_t ret = EFI_SUCCESS;
  598. struct file_handle *fh = to_fh(file);
  599. EFI_ENTRY("%p, %p", file, pos);
  600. if (fh->isdir) {
  601. ret = EFI_UNSUPPORTED;
  602. goto out;
  603. }
  604. *pos = fh->offset;
  605. out:
  606. return EFI_EXIT(ret);
  607. }
  608. /**
  609. * efi_file_setpos() - set current position in file
  610. *
  611. * This function implements the SetPosition service of the EFI file protocol.
  612. * See the UEFI spec for details.
  613. *
  614. * @file: file handle
  615. * @pos: new file position
  616. * Return: status code
  617. */
  618. static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file,
  619. u64 pos)
  620. {
  621. struct file_handle *fh = to_fh(file);
  622. efi_status_t ret = EFI_SUCCESS;
  623. EFI_ENTRY("%p, %llu", file, pos);
  624. if (fh->isdir) {
  625. if (pos != 0) {
  626. ret = EFI_UNSUPPORTED;
  627. goto error;
  628. }
  629. fs_closedir(fh->dirs);
  630. fh->dirs = NULL;
  631. }
  632. if (pos == ~0ULL) {
  633. loff_t file_size;
  634. ret = efi_get_file_size(fh, &file_size);
  635. if (ret != EFI_SUCCESS)
  636. goto error;
  637. pos = file_size;
  638. }
  639. fh->offset = pos;
  640. error:
  641. return EFI_EXIT(ret);
  642. }
  643. static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
  644. const efi_guid_t *info_type,
  645. efi_uintn_t *buffer_size,
  646. void *buffer)
  647. {
  648. struct file_handle *fh = to_fh(file);
  649. efi_status_t ret = EFI_SUCCESS;
  650. u16 *dst;
  651. EFI_ENTRY("%p, %pUl, %p, %p", file, info_type, buffer_size, buffer);
  652. if (!file || !info_type || !buffer_size ||
  653. (*buffer_size && !buffer)) {
  654. ret = EFI_INVALID_PARAMETER;
  655. goto error;
  656. }
  657. if (!guidcmp(info_type, &efi_file_info_guid)) {
  658. struct efi_file_info *info = buffer;
  659. char *filename = basename(fh);
  660. unsigned int required_size;
  661. loff_t file_size;
  662. /* check buffer size: */
  663. required_size = sizeof(*info) +
  664. 2 * (utf8_utf16_strlen(filename) + 1);
  665. if (*buffer_size < required_size) {
  666. *buffer_size = required_size;
  667. ret = EFI_BUFFER_TOO_SMALL;
  668. goto error;
  669. }
  670. ret = efi_get_file_size(fh, &file_size);
  671. if (ret != EFI_SUCCESS)
  672. goto error;
  673. memset(info, 0, required_size);
  674. info->size = required_size;
  675. info->file_size = file_size;
  676. info->physical_size = file_size;
  677. if (fh->isdir)
  678. info->attribute |= EFI_FILE_DIRECTORY;
  679. dst = info->file_name;
  680. utf8_utf16_strcpy(&dst, filename);
  681. } else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
  682. struct efi_file_system_info *info = buffer;
  683. struct disk_partition part;
  684. efi_uintn_t required_size;
  685. int r;
  686. if (fh->fs->part >= 1)
  687. r = part_get_info(fh->fs->desc, fh->fs->part, &part);
  688. else
  689. r = part_get_info_whole_disk(fh->fs->desc, &part);
  690. if (r < 0) {
  691. ret = EFI_DEVICE_ERROR;
  692. goto error;
  693. }
  694. required_size = sizeof(*info) + 2;
  695. if (*buffer_size < required_size) {
  696. *buffer_size = required_size;
  697. ret = EFI_BUFFER_TOO_SMALL;
  698. goto error;
  699. }
  700. memset(info, 0, required_size);
  701. info->size = required_size;
  702. /*
  703. * TODO: We cannot determine if the volume can be written to.
  704. */
  705. info->read_only = false;
  706. info->volume_size = part.size * part.blksz;
  707. /*
  708. * TODO: We currently have no function to determine the free
  709. * space. The volume size is the best upper bound we have.
  710. */
  711. info->free_space = info->volume_size;
  712. info->block_size = part.blksz;
  713. /*
  714. * TODO: The volume label is not available in U-Boot.
  715. */
  716. info->volume_label[0] = 0;
  717. } else if (!guidcmp(info_type, &efi_system_volume_label_id)) {
  718. if (*buffer_size < 2) {
  719. *buffer_size = 2;
  720. ret = EFI_BUFFER_TOO_SMALL;
  721. goto error;
  722. }
  723. *(u16 *)buffer = 0;
  724. } else {
  725. ret = EFI_UNSUPPORTED;
  726. }
  727. error:
  728. return EFI_EXIT(ret);
  729. }
  730. static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
  731. const efi_guid_t *info_type,
  732. efi_uintn_t buffer_size,
  733. void *buffer)
  734. {
  735. struct file_handle *fh = to_fh(file);
  736. efi_status_t ret = EFI_UNSUPPORTED;
  737. EFI_ENTRY("%p, %pUl, %zu, %p", file, info_type, buffer_size, buffer);
  738. if (!guidcmp(info_type, &efi_file_info_guid)) {
  739. struct efi_file_info *info = (struct efi_file_info *)buffer;
  740. char *filename = basename(fh);
  741. char *new_file_name, *pos;
  742. loff_t file_size;
  743. /* The buffer will always contain a file name. */
  744. if (buffer_size < sizeof(struct efi_file_info) + 2 ||
  745. buffer_size < info->size) {
  746. ret = EFI_BAD_BUFFER_SIZE;
  747. goto out;
  748. }
  749. /* We cannot change the directory attribute */
  750. if (!fh->isdir != !(info->attribute & EFI_FILE_DIRECTORY)) {
  751. ret = EFI_ACCESS_DENIED;
  752. goto out;
  753. }
  754. /* Check for renaming */
  755. new_file_name = malloc(utf16_utf8_strlen(info->file_name) + 1);
  756. if (!new_file_name) {
  757. ret = EFI_OUT_OF_RESOURCES;
  758. goto out;
  759. }
  760. pos = new_file_name;
  761. utf16_utf8_strcpy(&pos, info->file_name);
  762. if (strcmp(new_file_name, filename)) {
  763. /* TODO: we do not support renaming */
  764. EFI_PRINT("Renaming not supported\n");
  765. free(new_file_name);
  766. ret = EFI_ACCESS_DENIED;
  767. goto out;
  768. }
  769. free(new_file_name);
  770. /* Check for truncation */
  771. ret = efi_get_file_size(fh, &file_size);
  772. if (ret != EFI_SUCCESS)
  773. goto out;
  774. if (file_size != info->file_size) {
  775. /* TODO: we do not support truncation */
  776. EFI_PRINT("Truncation not supported\n");
  777. ret = EFI_ACCESS_DENIED;
  778. goto out;
  779. }
  780. /*
  781. * We do not care for the other attributes
  782. * TODO: Support read only
  783. */
  784. ret = EFI_SUCCESS;
  785. } else {
  786. /* TODO: We do not support changing the volume label */
  787. ret = EFI_UNSUPPORTED;
  788. }
  789. out:
  790. return EFI_EXIT(ret);
  791. }
  792. /**
  793. * efi_file_flush_int() - flush file
  794. *
  795. * This is the internal implementation of the Flush() and FlushEx() services of
  796. * the EFI_FILE_PROTOCOL.
  797. *
  798. * @this: file protocol instance
  799. * Return: status code
  800. */
  801. static efi_status_t efi_file_flush_int(struct efi_file_handle *this)
  802. {
  803. struct file_handle *fh = to_fh(this);
  804. if (!this)
  805. return EFI_INVALID_PARAMETER;
  806. if (!(fh->open_mode & EFI_FILE_MODE_WRITE))
  807. return EFI_ACCESS_DENIED;
  808. /* TODO: flush for file position after end of file */
  809. return EFI_SUCCESS;
  810. }
  811. /**
  812. * efi_file_flush() - flush file
  813. *
  814. * This function implements the Flush() service of the EFI_FILE_PROTOCOL.
  815. *
  816. * See the Unified Extensible Firmware Interface (UEFI) specification for
  817. * details.
  818. *
  819. * @this: file protocol instance
  820. * Return: status code
  821. */
  822. static efi_status_t EFIAPI efi_file_flush(struct efi_file_handle *this)
  823. {
  824. efi_status_t ret;
  825. EFI_ENTRY("%p", this);
  826. ret = efi_file_flush_int(this);
  827. return EFI_EXIT(ret);
  828. }
  829. /**
  830. * efi_file_flush_ex() - flush file
  831. *
  832. * This function implements the FlushEx() service of the EFI_FILE_PROTOCOL.
  833. *
  834. * See the Unified Extensible Firmware Interface (UEFI) specification for
  835. * details.
  836. *
  837. * @this: file protocol instance
  838. * @token: transaction token
  839. * Return: status code
  840. */
  841. static efi_status_t EFIAPI efi_file_flush_ex(struct efi_file_handle *this,
  842. struct efi_file_io_token *token)
  843. {
  844. efi_status_t ret;
  845. EFI_ENTRY("%p, %p", this, token);
  846. if (!token) {
  847. ret = EFI_INVALID_PARAMETER;
  848. goto out;
  849. }
  850. ret = efi_file_flush_int(this);
  851. if (ret == EFI_SUCCESS && token->event) {
  852. token->status = EFI_SUCCESS;
  853. efi_signal_event(token->event);
  854. }
  855. out:
  856. return EFI_EXIT(ret);
  857. }
  858. static const struct efi_file_handle efi_file_handle_protocol = {
  859. .rev = EFI_FILE_PROTOCOL_REVISION2,
  860. .open = efi_file_open,
  861. .close = efi_file_close,
  862. .delete = efi_file_delete,
  863. .read = efi_file_read,
  864. .write = efi_file_write,
  865. .getpos = efi_file_getpos,
  866. .setpos = efi_file_setpos,
  867. .getinfo = efi_file_getinfo,
  868. .setinfo = efi_file_setinfo,
  869. .flush = efi_file_flush,
  870. .open_ex = efi_file_open_ex,
  871. .read_ex = efi_file_read_ex,
  872. .write_ex = efi_file_write_ex,
  873. .flush_ex = efi_file_flush_ex,
  874. };
  875. /**
  876. * efi_file_from_path() - open file via device path
  877. *
  878. * @fp: device path
  879. * @return: EFI_FILE_PROTOCOL for the file or NULL
  880. */
  881. struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp)
  882. {
  883. struct efi_simple_file_system_protocol *v;
  884. struct efi_file_handle *f;
  885. efi_status_t ret;
  886. v = efi_fs_from_path(fp);
  887. if (!v)
  888. return NULL;
  889. EFI_CALL(ret = v->open_volume(v, &f));
  890. if (ret != EFI_SUCCESS)
  891. return NULL;
  892. /* Skip over device-path nodes before the file path. */
  893. while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH))
  894. fp = efi_dp_next(fp);
  895. /*
  896. * Step through the nodes of the directory path until the actual file
  897. * node is reached which is the final node in the device path.
  898. */
  899. while (fp) {
  900. struct efi_device_path_file_path *fdp =
  901. container_of(fp, struct efi_device_path_file_path, dp);
  902. struct efi_file_handle *f2;
  903. u16 *filename;
  904. if (!EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) {
  905. printf("bad file path!\n");
  906. f->close(f);
  907. return NULL;
  908. }
  909. filename = u16_strdup(fdp->str);
  910. if (!filename)
  911. return NULL;
  912. EFI_CALL(ret = f->open(f, &f2, filename,
  913. EFI_FILE_MODE_READ, 0));
  914. free(filename);
  915. if (ret != EFI_SUCCESS)
  916. return NULL;
  917. fp = efi_dp_next(fp);
  918. EFI_CALL(f->close(f));
  919. f = f2;
  920. }
  921. return f;
  922. }
  923. static efi_status_t EFIAPI
  924. efi_open_volume(struct efi_simple_file_system_protocol *this,
  925. struct efi_file_handle **root)
  926. {
  927. struct file_system *fs = to_fs(this);
  928. EFI_ENTRY("%p, %p", this, root);
  929. *root = file_open(fs, NULL, NULL, 0, 0);
  930. return EFI_EXIT(EFI_SUCCESS);
  931. }
  932. struct efi_simple_file_system_protocol *
  933. efi_simple_file_system(struct blk_desc *desc, int part,
  934. struct efi_device_path *dp)
  935. {
  936. struct file_system *fs;
  937. fs = calloc(1, sizeof(*fs));
  938. fs->base.rev = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION;
  939. fs->base.open_volume = efi_open_volume;
  940. fs->desc = desc;
  941. fs->part = part;
  942. fs->dp = dp;
  943. return &fs->base;
  944. }