efi_file.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168
  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. /**
  350. * efi_file_size() - Get the size of a file using an EFI file handle
  351. *
  352. * @fh: EFI file handle
  353. * @size: buffer to fill in the discovered size
  354. *
  355. * Return: size of the file
  356. */
  357. efi_status_t efi_file_size(struct efi_file_handle *fh, efi_uintn_t *size)
  358. {
  359. struct efi_file_info *info = NULL;
  360. efi_uintn_t bs = 0;
  361. efi_status_t ret;
  362. *size = 0;
  363. ret = EFI_CALL(fh->getinfo(fh, (efi_guid_t *)&efi_file_info_guid, &bs,
  364. info));
  365. if (ret != EFI_BUFFER_TOO_SMALL) {
  366. ret = EFI_DEVICE_ERROR;
  367. goto out;
  368. }
  369. info = malloc(bs);
  370. if (!info) {
  371. ret = EFI_OUT_OF_RESOURCES;
  372. goto out;
  373. }
  374. ret = EFI_CALL(fh->getinfo(fh, (efi_guid_t *)&efi_file_info_guid, &bs,
  375. info));
  376. if (ret != EFI_SUCCESS)
  377. goto out;
  378. *size = info->file_size;
  379. out:
  380. free(info);
  381. return ret;
  382. }
  383. static efi_status_t file_read(struct file_handle *fh, u64 *buffer_size,
  384. void *buffer)
  385. {
  386. loff_t actread;
  387. efi_status_t ret;
  388. loff_t file_size;
  389. if (!buffer) {
  390. ret = EFI_INVALID_PARAMETER;
  391. return ret;
  392. }
  393. ret = efi_get_file_size(fh, &file_size);
  394. if (ret != EFI_SUCCESS)
  395. return ret;
  396. if (file_size < fh->offset) {
  397. ret = EFI_DEVICE_ERROR;
  398. return ret;
  399. }
  400. if (set_blk_dev(fh))
  401. return EFI_DEVICE_ERROR;
  402. if (fs_read(fh->path, map_to_sysmem(buffer), fh->offset,
  403. *buffer_size, &actread))
  404. return EFI_DEVICE_ERROR;
  405. *buffer_size = actread;
  406. fh->offset += actread;
  407. return EFI_SUCCESS;
  408. }
  409. static void rtc2efi(struct efi_time *time, struct rtc_time *tm)
  410. {
  411. memset(time, 0, sizeof(struct efi_time));
  412. time->year = tm->tm_year;
  413. time->month = tm->tm_mon;
  414. time->day = tm->tm_mday;
  415. time->hour = tm->tm_hour;
  416. time->minute = tm->tm_min;
  417. time->second = tm->tm_sec;
  418. }
  419. static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size,
  420. void *buffer)
  421. {
  422. struct efi_file_info *info = buffer;
  423. struct fs_dirent *dent;
  424. u64 required_size;
  425. u16 *dst;
  426. if (set_blk_dev(fh))
  427. return EFI_DEVICE_ERROR;
  428. if (!fh->dirs) {
  429. assert(fh->offset == 0);
  430. fh->dirs = fs_opendir(fh->path);
  431. if (!fh->dirs)
  432. return EFI_DEVICE_ERROR;
  433. fh->dent = NULL;
  434. }
  435. /*
  436. * So this is a bit awkward. Since fs layer is stateful and we
  437. * can't rewind an entry, in the EFI_BUFFER_TOO_SMALL case below
  438. * we might have to return without consuming the dent.. so we
  439. * have to stash it for next call.
  440. */
  441. if (fh->dent) {
  442. dent = fh->dent;
  443. } else {
  444. dent = fs_readdir(fh->dirs);
  445. }
  446. if (!dent) {
  447. /* no more files in directory */
  448. *buffer_size = 0;
  449. return EFI_SUCCESS;
  450. }
  451. /* check buffer size: */
  452. required_size = sizeof(*info) +
  453. 2 * (utf8_utf16_strlen(dent->name) + 1);
  454. if (*buffer_size < required_size) {
  455. *buffer_size = required_size;
  456. fh->dent = dent;
  457. return EFI_BUFFER_TOO_SMALL;
  458. }
  459. if (!buffer)
  460. return EFI_INVALID_PARAMETER;
  461. fh->dent = NULL;
  462. *buffer_size = required_size;
  463. memset(info, 0, required_size);
  464. info->size = required_size;
  465. info->file_size = dent->size;
  466. info->physical_size = dent->size;
  467. info->attribute = dent->attr;
  468. rtc2efi(&info->create_time, &dent->create_time);
  469. rtc2efi(&info->modification_time, &dent->change_time);
  470. rtc2efi(&info->last_access_time, &dent->access_time);
  471. if (dent->type == FS_DT_DIR)
  472. info->attribute |= EFI_FILE_DIRECTORY;
  473. dst = info->file_name;
  474. utf8_utf16_strcpy(&dst, dent->name);
  475. fh->offset++;
  476. return EFI_SUCCESS;
  477. }
  478. static efi_status_t efi_file_read_int(struct efi_file_handle *this,
  479. efi_uintn_t *buffer_size, void *buffer)
  480. {
  481. struct file_handle *fh = to_fh(this);
  482. efi_status_t ret = EFI_SUCCESS;
  483. u64 bs;
  484. if (!this || !buffer_size)
  485. return EFI_INVALID_PARAMETER;
  486. bs = *buffer_size;
  487. if (fh->isdir)
  488. ret = dir_read(fh, &bs, buffer);
  489. else
  490. ret = file_read(fh, &bs, buffer);
  491. if (bs <= SIZE_MAX)
  492. *buffer_size = bs;
  493. else
  494. *buffer_size = SIZE_MAX;
  495. return ret;
  496. }
  497. /**
  498. * efi_file_read() - read file
  499. *
  500. * This function implements the Read() service of the EFI_FILE_PROTOCOL.
  501. *
  502. * See the Unified Extensible Firmware Interface (UEFI) specification for
  503. * details.
  504. *
  505. * @this: file protocol instance
  506. * @buffer_size: number of bytes to read
  507. * @buffer: read buffer
  508. * Return: status code
  509. */
  510. static efi_status_t EFIAPI efi_file_read(struct efi_file_handle *this,
  511. efi_uintn_t *buffer_size, void *buffer)
  512. {
  513. efi_status_t ret;
  514. EFI_ENTRY("%p, %p, %p", this, buffer_size, buffer);
  515. ret = efi_file_read_int(this, buffer_size, buffer);
  516. return EFI_EXIT(ret);
  517. }
  518. /**
  519. * efi_file_read_ex() - read file asynchonously
  520. *
  521. * This function implements the ReadEx() service of the EFI_FILE_PROTOCOL.
  522. *
  523. * See the Unified Extensible Firmware Interface (UEFI) specification for
  524. * details.
  525. *
  526. * @this: file protocol instance
  527. * @token: transaction token
  528. * Return: status code
  529. */
  530. static efi_status_t EFIAPI efi_file_read_ex(struct efi_file_handle *this,
  531. struct efi_file_io_token *token)
  532. {
  533. efi_status_t ret;
  534. EFI_ENTRY("%p, %p", this, token);
  535. if (!token) {
  536. ret = EFI_INVALID_PARAMETER;
  537. goto out;
  538. }
  539. ret = efi_file_read_int(this, &token->buffer_size, token->buffer);
  540. if (ret == EFI_SUCCESS && token->event) {
  541. token->status = EFI_SUCCESS;
  542. efi_signal_event(token->event);
  543. }
  544. out:
  545. return EFI_EXIT(ret);
  546. }
  547. static efi_status_t efi_file_write_int(struct efi_file_handle *this,
  548. efi_uintn_t *buffer_size, void *buffer)
  549. {
  550. struct file_handle *fh = to_fh(this);
  551. efi_status_t ret = EFI_SUCCESS;
  552. loff_t actwrite;
  553. if (!this || !buffer_size || !buffer) {
  554. ret = EFI_INVALID_PARAMETER;
  555. goto out;
  556. }
  557. if (fh->isdir) {
  558. ret = EFI_UNSUPPORTED;
  559. goto out;
  560. }
  561. if (!(fh->open_mode & EFI_FILE_MODE_WRITE)) {
  562. ret = EFI_ACCESS_DENIED;
  563. goto out;
  564. }
  565. if (!*buffer_size)
  566. goto out;
  567. if (set_blk_dev(fh)) {
  568. ret = EFI_DEVICE_ERROR;
  569. goto out;
  570. }
  571. if (fs_write(fh->path, map_to_sysmem(buffer), fh->offset, *buffer_size,
  572. &actwrite)) {
  573. ret = EFI_DEVICE_ERROR;
  574. goto out;
  575. }
  576. *buffer_size = actwrite;
  577. fh->offset += actwrite;
  578. out:
  579. return ret;
  580. }
  581. /**
  582. * efi_file_write() - write to file
  583. *
  584. * This function implements the Write() service of the EFI_FILE_PROTOCOL.
  585. *
  586. * See the Unified Extensible Firmware Interface (UEFI) specification for
  587. * details.
  588. *
  589. * @this: file protocol instance
  590. * @buffer_size: number of bytes to write
  591. * @buffer: buffer with the bytes to write
  592. * Return: status code
  593. */
  594. static efi_status_t EFIAPI efi_file_write(struct efi_file_handle *this,
  595. efi_uintn_t *buffer_size,
  596. void *buffer)
  597. {
  598. efi_status_t ret;
  599. EFI_ENTRY("%p, %p, %p", this, buffer_size, buffer);
  600. ret = efi_file_write_int(this, buffer_size, buffer);
  601. return EFI_EXIT(ret);
  602. }
  603. /**
  604. * efi_file_write_ex() - write to file
  605. *
  606. * This function implements the WriteEx() service of the EFI_FILE_PROTOCOL.
  607. *
  608. * See the Unified Extensible Firmware Interface (UEFI) specification for
  609. * details.
  610. *
  611. * @this: file protocol instance
  612. * @token: transaction token
  613. * Return: status code
  614. */
  615. static efi_status_t EFIAPI efi_file_write_ex(struct efi_file_handle *this,
  616. struct efi_file_io_token *token)
  617. {
  618. efi_status_t ret;
  619. EFI_ENTRY("%p, %p", this, token);
  620. if (!token) {
  621. ret = EFI_INVALID_PARAMETER;
  622. goto out;
  623. }
  624. ret = efi_file_write_int(this, &token->buffer_size, token->buffer);
  625. if (ret == EFI_SUCCESS && token->event) {
  626. token->status = EFI_SUCCESS;
  627. efi_signal_event(token->event);
  628. }
  629. out:
  630. return EFI_EXIT(ret);
  631. }
  632. /**
  633. * efi_file_getpos() - get current position in file
  634. *
  635. * This function implements the GetPosition service of the EFI file protocol.
  636. * See the UEFI spec for details.
  637. *
  638. * @file: file handle
  639. * @pos: pointer to file position
  640. * Return: status code
  641. */
  642. static efi_status_t EFIAPI efi_file_getpos(struct efi_file_handle *file,
  643. u64 *pos)
  644. {
  645. efi_status_t ret = EFI_SUCCESS;
  646. struct file_handle *fh = to_fh(file);
  647. EFI_ENTRY("%p, %p", file, pos);
  648. if (fh->isdir) {
  649. ret = EFI_UNSUPPORTED;
  650. goto out;
  651. }
  652. *pos = fh->offset;
  653. out:
  654. return EFI_EXIT(ret);
  655. }
  656. /**
  657. * efi_file_setpos() - set current position in file
  658. *
  659. * This function implements the SetPosition service of the EFI file protocol.
  660. * See the UEFI spec for details.
  661. *
  662. * @file: file handle
  663. * @pos: new file position
  664. * Return: status code
  665. */
  666. static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file,
  667. u64 pos)
  668. {
  669. struct file_handle *fh = to_fh(file);
  670. efi_status_t ret = EFI_SUCCESS;
  671. EFI_ENTRY("%p, %llu", file, pos);
  672. if (fh->isdir) {
  673. if (pos != 0) {
  674. ret = EFI_UNSUPPORTED;
  675. goto error;
  676. }
  677. fs_closedir(fh->dirs);
  678. fh->dirs = NULL;
  679. }
  680. if (pos == ~0ULL) {
  681. loff_t file_size;
  682. ret = efi_get_file_size(fh, &file_size);
  683. if (ret != EFI_SUCCESS)
  684. goto error;
  685. pos = file_size;
  686. }
  687. fh->offset = pos;
  688. error:
  689. return EFI_EXIT(ret);
  690. }
  691. static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file,
  692. const efi_guid_t *info_type,
  693. efi_uintn_t *buffer_size,
  694. void *buffer)
  695. {
  696. struct file_handle *fh = to_fh(file);
  697. efi_status_t ret = EFI_SUCCESS;
  698. u16 *dst;
  699. EFI_ENTRY("%p, %pUs, %p, %p", file, info_type, buffer_size, buffer);
  700. if (!file || !info_type || !buffer_size ||
  701. (*buffer_size && !buffer)) {
  702. ret = EFI_INVALID_PARAMETER;
  703. goto error;
  704. }
  705. if (!guidcmp(info_type, &efi_file_info_guid)) {
  706. struct efi_file_info *info = buffer;
  707. char *filename = basename(fh);
  708. unsigned int required_size;
  709. loff_t file_size;
  710. /* check buffer size: */
  711. required_size = sizeof(*info) +
  712. 2 * (utf8_utf16_strlen(filename) + 1);
  713. if (*buffer_size < required_size) {
  714. *buffer_size = required_size;
  715. ret = EFI_BUFFER_TOO_SMALL;
  716. goto error;
  717. }
  718. ret = efi_get_file_size(fh, &file_size);
  719. if (ret != EFI_SUCCESS)
  720. goto error;
  721. memset(info, 0, required_size);
  722. info->size = required_size;
  723. info->file_size = file_size;
  724. info->physical_size = file_size;
  725. if (fh->isdir)
  726. info->attribute |= EFI_FILE_DIRECTORY;
  727. dst = info->file_name;
  728. utf8_utf16_strcpy(&dst, filename);
  729. } else if (!guidcmp(info_type, &efi_file_system_info_guid)) {
  730. struct efi_file_system_info *info = buffer;
  731. struct disk_partition part;
  732. efi_uintn_t required_size;
  733. int r;
  734. if (fh->fs->part >= 1)
  735. r = part_get_info(fh->fs->desc, fh->fs->part, &part);
  736. else
  737. r = part_get_info_whole_disk(fh->fs->desc, &part);
  738. if (r < 0) {
  739. ret = EFI_DEVICE_ERROR;
  740. goto error;
  741. }
  742. required_size = sizeof(*info) + 2;
  743. if (*buffer_size < required_size) {
  744. *buffer_size = required_size;
  745. ret = EFI_BUFFER_TOO_SMALL;
  746. goto error;
  747. }
  748. memset(info, 0, required_size);
  749. info->size = required_size;
  750. /*
  751. * TODO: We cannot determine if the volume can be written to.
  752. */
  753. info->read_only = false;
  754. info->volume_size = part.size * part.blksz;
  755. /*
  756. * TODO: We currently have no function to determine the free
  757. * space. The volume size is the best upper bound we have.
  758. */
  759. info->free_space = info->volume_size;
  760. info->block_size = part.blksz;
  761. /*
  762. * TODO: The volume label is not available in U-Boot.
  763. */
  764. info->volume_label[0] = 0;
  765. } else if (!guidcmp(info_type, &efi_system_volume_label_id)) {
  766. if (*buffer_size < 2) {
  767. *buffer_size = 2;
  768. ret = EFI_BUFFER_TOO_SMALL;
  769. goto error;
  770. }
  771. *(u16 *)buffer = 0;
  772. } else {
  773. ret = EFI_UNSUPPORTED;
  774. }
  775. error:
  776. return EFI_EXIT(ret);
  777. }
  778. static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
  779. const efi_guid_t *info_type,
  780. efi_uintn_t buffer_size,
  781. void *buffer)
  782. {
  783. struct file_handle *fh = to_fh(file);
  784. efi_status_t ret = EFI_UNSUPPORTED;
  785. EFI_ENTRY("%p, %pUs, %zu, %p", file, info_type, buffer_size, buffer);
  786. if (!guidcmp(info_type, &efi_file_info_guid)) {
  787. struct efi_file_info *info = (struct efi_file_info *)buffer;
  788. char *filename = basename(fh);
  789. char *new_file_name, *pos;
  790. loff_t file_size;
  791. /* The buffer will always contain a file name. */
  792. if (buffer_size < sizeof(struct efi_file_info) + 2 ||
  793. buffer_size < info->size) {
  794. ret = EFI_BAD_BUFFER_SIZE;
  795. goto out;
  796. }
  797. /* We cannot change the directory attribute */
  798. if (!fh->isdir != !(info->attribute & EFI_FILE_DIRECTORY)) {
  799. ret = EFI_ACCESS_DENIED;
  800. goto out;
  801. }
  802. /* Check for renaming */
  803. new_file_name = malloc(utf16_utf8_strlen(info->file_name) + 1);
  804. if (!new_file_name) {
  805. ret = EFI_OUT_OF_RESOURCES;
  806. goto out;
  807. }
  808. pos = new_file_name;
  809. utf16_utf8_strcpy(&pos, info->file_name);
  810. if (strcmp(new_file_name, filename)) {
  811. /* TODO: we do not support renaming */
  812. EFI_PRINT("Renaming not supported\n");
  813. free(new_file_name);
  814. ret = EFI_ACCESS_DENIED;
  815. goto out;
  816. }
  817. free(new_file_name);
  818. /* Check for truncation */
  819. ret = efi_get_file_size(fh, &file_size);
  820. if (ret != EFI_SUCCESS)
  821. goto out;
  822. if (file_size != info->file_size) {
  823. /* TODO: we do not support truncation */
  824. EFI_PRINT("Truncation not supported\n");
  825. ret = EFI_ACCESS_DENIED;
  826. goto out;
  827. }
  828. /*
  829. * We do not care for the other attributes
  830. * TODO: Support read only
  831. */
  832. ret = EFI_SUCCESS;
  833. } else {
  834. /* TODO: We do not support changing the volume label */
  835. ret = EFI_UNSUPPORTED;
  836. }
  837. out:
  838. return EFI_EXIT(ret);
  839. }
  840. /**
  841. * efi_file_flush_int() - flush file
  842. *
  843. * This is the internal implementation of the Flush() and FlushEx() services of
  844. * the EFI_FILE_PROTOCOL.
  845. *
  846. * @this: file protocol instance
  847. * Return: status code
  848. */
  849. static efi_status_t efi_file_flush_int(struct efi_file_handle *this)
  850. {
  851. struct file_handle *fh = to_fh(this);
  852. if (!this)
  853. return EFI_INVALID_PARAMETER;
  854. if (!(fh->open_mode & EFI_FILE_MODE_WRITE))
  855. return EFI_ACCESS_DENIED;
  856. /* TODO: flush for file position after end of file */
  857. return EFI_SUCCESS;
  858. }
  859. /**
  860. * efi_file_flush() - flush file
  861. *
  862. * This function implements the Flush() service of the EFI_FILE_PROTOCOL.
  863. *
  864. * See the Unified Extensible Firmware Interface (UEFI) specification for
  865. * details.
  866. *
  867. * @this: file protocol instance
  868. * Return: status code
  869. */
  870. static efi_status_t EFIAPI efi_file_flush(struct efi_file_handle *this)
  871. {
  872. efi_status_t ret;
  873. EFI_ENTRY("%p", this);
  874. ret = efi_file_flush_int(this);
  875. return EFI_EXIT(ret);
  876. }
  877. /**
  878. * efi_file_flush_ex() - flush file
  879. *
  880. * This function implements the FlushEx() service of the EFI_FILE_PROTOCOL.
  881. *
  882. * See the Unified Extensible Firmware Interface (UEFI) specification for
  883. * details.
  884. *
  885. * @this: file protocol instance
  886. * @token: transaction token
  887. * Return: status code
  888. */
  889. static efi_status_t EFIAPI efi_file_flush_ex(struct efi_file_handle *this,
  890. struct efi_file_io_token *token)
  891. {
  892. efi_status_t ret;
  893. EFI_ENTRY("%p, %p", this, token);
  894. if (!token) {
  895. ret = EFI_INVALID_PARAMETER;
  896. goto out;
  897. }
  898. ret = efi_file_flush_int(this);
  899. if (ret == EFI_SUCCESS && token->event) {
  900. token->status = EFI_SUCCESS;
  901. efi_signal_event(token->event);
  902. }
  903. out:
  904. return EFI_EXIT(ret);
  905. }
  906. static const struct efi_file_handle efi_file_handle_protocol = {
  907. .rev = EFI_FILE_PROTOCOL_REVISION2,
  908. .open = efi_file_open,
  909. .close = efi_file_close,
  910. .delete = efi_file_delete,
  911. .read = efi_file_read,
  912. .write = efi_file_write,
  913. .getpos = efi_file_getpos,
  914. .setpos = efi_file_setpos,
  915. .getinfo = efi_file_getinfo,
  916. .setinfo = efi_file_setinfo,
  917. .flush = efi_file_flush,
  918. .open_ex = efi_file_open_ex,
  919. .read_ex = efi_file_read_ex,
  920. .write_ex = efi_file_write_ex,
  921. .flush_ex = efi_file_flush_ex,
  922. };
  923. /**
  924. * efi_file_from_path() - open file via device path
  925. *
  926. * @fp: device path
  927. * @return: EFI_FILE_PROTOCOL for the file or NULL
  928. */
  929. struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp)
  930. {
  931. struct efi_simple_file_system_protocol *v;
  932. struct efi_file_handle *f;
  933. efi_status_t ret;
  934. v = efi_fs_from_path(fp);
  935. if (!v)
  936. return NULL;
  937. EFI_CALL(ret = v->open_volume(v, &f));
  938. if (ret != EFI_SUCCESS)
  939. return NULL;
  940. /* Skip over device-path nodes before the file path. */
  941. while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH))
  942. fp = efi_dp_next(fp);
  943. /*
  944. * Step through the nodes of the directory path until the actual file
  945. * node is reached which is the final node in the device path.
  946. */
  947. while (fp) {
  948. struct efi_device_path_file_path *fdp =
  949. container_of(fp, struct efi_device_path_file_path, dp);
  950. struct efi_file_handle *f2;
  951. u16 *filename;
  952. if (!EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) {
  953. printf("bad file path!\n");
  954. f->close(f);
  955. return NULL;
  956. }
  957. filename = u16_strdup(fdp->str);
  958. if (!filename)
  959. return NULL;
  960. EFI_CALL(ret = f->open(f, &f2, filename,
  961. EFI_FILE_MODE_READ, 0));
  962. free(filename);
  963. if (ret != EFI_SUCCESS)
  964. return NULL;
  965. fp = efi_dp_next(fp);
  966. EFI_CALL(f->close(f));
  967. f = f2;
  968. }
  969. return f;
  970. }
  971. static efi_status_t EFIAPI
  972. efi_open_volume(struct efi_simple_file_system_protocol *this,
  973. struct efi_file_handle **root)
  974. {
  975. struct file_system *fs = to_fs(this);
  976. EFI_ENTRY("%p, %p", this, root);
  977. *root = file_open(fs, NULL, NULL, 0, 0);
  978. return EFI_EXIT(EFI_SUCCESS);
  979. }
  980. struct efi_simple_file_system_protocol *
  981. efi_simple_file_system(struct blk_desc *desc, int part,
  982. struct efi_device_path *dp)
  983. {
  984. struct file_system *fs;
  985. fs = calloc(1, sizeof(*fs));
  986. fs->base.rev = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION;
  987. fs->base.open_volume = efi_open_volume;
  988. fs->desc = desc;
  989. fs->part = part;
  990. fs->dp = dp;
  991. return &fs->base;
  992. }