efi_file.c 27 KB

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