From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from list by lists.gnu.org with archive (Exim 4.71) id 1d6LFe-0003G0-NU for mharc-grub-devel@gnu.org; Thu, 04 May 2017 14:13:18 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46325) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d6LFW-0003AQ-CJ for grub-devel@gnu.org; Thu, 04 May 2017 14:13:15 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d6LFS-00053o-6p for grub-devel@gnu.org; Thu, 04 May 2017 14:13:10 -0400 Received: from mail.kernel.org ([198.145.29.136]:33246) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1d6LFR-00052h-Ou for grub-devel@gnu.org; Thu, 04 May 2017 14:13:06 -0400 Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 3A08D2039E; Thu, 4 May 2017 18:13:01 +0000 (UTC) Received: from localhost (107-1-141-74-ip-static.hfc.comcastbusiness.net [107.1.141.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id C735F2039D; Thu, 4 May 2017 18:12:57 +0000 (UTC) From: Jaegeuk Kim To: grub-devel@gnu.org, linux-f2fs-devel@lists.sourceforge.net Cc: Jaegeuk Kim Subject: [PATCH] F2FS support Date: Thu, 4 May 2017 11:12:40 -0700 Message-Id: <20170504181240.7923-1-jaegeuk@kernel.org> X-Mailer: git-send-email 2.11.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Virus-Scanned: ClamAV using ClamSMTP Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 198.145.29.136 X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 04 May 2017 18:13:15 -0000 "F2FS (Flash-Friendly File System) is flash-friendly file system which wa= s merged into Linux kernel v3.8 in 2013. The motive for F2FS was to build a file system that from the start, takes= into account the characteristics of NAND flash memory-based storage devices (s= uch as solid-state disks, eMMC, and SD cards). F2FS was designed on a basis of a log-structured file system approach, wh= ich remedies some known issues of the older log structured file systems, such= as the snowball effect of wandering trees and high cleaning overhead. In add= ition, since a NAND-based storage device shows different characteristics accordi= ng to its internal geometry or flash memory management scheme (such as the Flas= h Translation Layer or FTL), it supports various parameters not only for configuring on-disk layout, but also for selecting allocation and cleanin= g algorithm.", quote by https://en.wikipedia.org/wiki/F2FS. The source codes for F2FS are available from: http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs.git http://git.kernel.org/cgit/linux/kernel/git/jaegeuk/f2fs-tools.git Update: - This patch has been integrated in OpenMandriva Lx 3. https://www.openmandriva.org/ Acked-by: Andrei Borzenkov Signed-off-by: Jaegeuk Kim --- Makefile.util.def | 7 + docs/grub.texi | 5 +- grub-core/Makefile.core.def | 5 + grub-core/fs/f2fs.c | 1289 ++++++++++++++++++++++++++++++++++++= ++++++ po/exclude.pot | 1 + tests/f2fs_test.in | 19 + tests/util/grub-fs-tester.in | 10 +- 7 files changed, 1332 insertions(+), 4 deletions(-) create mode 100644 grub-core/fs/f2fs.c create mode 100644 tests/f2fs_test.in diff --git a/Makefile.util.def b/Makefile.util.def index f9caccb97..3180ac880 100644 --- a/Makefile.util.def +++ b/Makefile.util.def @@ -99,6 +99,7 @@ library =3D { common =3D grub-core/fs/ext2.c; common =3D grub-core/fs/fat.c; common =3D grub-core/fs/exfat.c; + common =3D grub-core/fs/f2fs.c; common =3D grub-core/fs/fshelp.c; common =3D grub-core/fs/hfs.c; common =3D grub-core/fs/hfsplus.c; @@ -776,6 +777,12 @@ script =3D { =20 script =3D { testcase; + name =3D f2fs_test; + common =3D tests/f2fs_test.in; +}; + +script =3D { + testcase; name =3D nilfs2_test; common =3D tests/nilfs2_test.in; }; diff --git a/docs/grub.texi b/docs/grub.texi index e935af33e..7137d6d0d 100644 --- a/docs/grub.texi +++ b/docs/grub.texi @@ -360,7 +360,8 @@ blocklist notation. The currently supported filesyste= m types are @dfn{Amiga Fast FileSystem (AFFS)}, @dfn{AtheOS fs}, @dfn{BeFS}, @dfn{BtrFS} (including raid0, raid1, raid10, gzip and lzo), @dfn{cpio} (little- and big-endian bin, odc and newc variants), -@dfn{Linux ext2/ext3/ext4}, @dfn{DOS FAT12/FAT16/FAT32}, @dfn{exFAT}, @d= fn{HFS}, +@dfn{Linux ext2/ext3/ext4}, @dfn{DOS FAT12/FAT16/FAT32}, @dfn{exFAT}, +@dfn{f2fs}, @dfn{HFS}, @dfn{HFS+}, @dfn{ISO9660} (including Joliet, Rock-ridge and multi-chunk = files), @dfn{JFS}, @dfn{Minix fs} (versions 1, 2 and 3), @dfn{nilfs2}, @dfn{NTFS} (including compression), @dfn{ReiserFS}, @dfn{ROMFS}, @@ -5368,7 +5369,7 @@ NTFS, JFS, UDF, HFS+, exFAT, long filenames in FAT,= Joliet part of ISO9660 are treated as UTF-16 as per specification. AFS and BFS are read as UTF-8, again according to specification. BtrFS, cpio, tar, squash4, m= inix, minix2, minix3, ROMFS, ReiserFS, XFS, ext2, ext3, ext4, FAT (short names= ), -RockRidge part of ISO9660, nilfs2, UFS1, UFS2 and ZFS are assumed +f2fs, RockRidge part of ISO9660, nilfs2, UFS1, UFS2 and ZFS are assumed to be UTF-8. This might be false on systems configured with legacy chars= et but as long as the charset used is superset of ASCII you should be able = to access ASCII-named files. And it's recommended to configure your system = to use diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def index 2dfa22a92..7f084d466 100644 --- a/grub-core/Makefile.core.def +++ b/grub-core/Makefile.core.def @@ -1291,6 +1291,11 @@ module =3D { }; =20 module =3D { + name =3D f2fs; + common =3D fs/f2fs.c; +}; + +module =3D { name =3D fshelp; common =3D fs/fshelp.c; }; diff --git a/grub-core/fs/f2fs.c b/grub-core/fs/f2fs.c new file mode 100644 index 000000000..7fb256f20 --- /dev/null +++ b/grub-core/fs/f2fs.c @@ -0,0 +1,1289 @@ +/* + * f2fs.c - Flash-Friendly File System + * + * Written by Jaegeuk Kim + * + * Copyright (C) 2015 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +GRUB_MOD_LICENSE ("GPLv3+"); + +/* F2FS Magic Number */ +#define F2FS_SUPER_MAGIC 0xF2F52010 +#define CHECKSUM_OFFSET 4092 /* must be aligned 4 bytes */ +#define U32_CHECKSUM_OFFSET (CHECKSUM_OFFSET >> 2) + +/* byte-size offset */ +#define F2FS_SUPER_OFFSET ((grub_disk_addr_t)1024) +#define F2FS_SUPER_OFFSET0 (F2FS_SUPER_OFFSET >> GRUB_DISK_SECTOR_BITS) +#define F2FS_SUPER_OFFSET1 ((F2FS_SUPER_OFFSET + F2FS_BLKSIZE) >> \ + GRUB_DISK_SECTOR_BITS) + +/* 9 bits for 512 bytes */ +#define F2FS_MIN_LOG_SECTOR_SIZE 9 + +/* support only 4KB block */ +#define F2FS_BLK_BITS 12 +#define F2FS_BLKSIZE (1 << F2FS_BLK_BITS) +#define F2FS_BLK_SEC_BITS (F2FS_BLK_BITS - GRUB_DISK_SECTOR_BITS) + +#define VERSION_LEN 256 +#define F2FS_MAX_EXTENSION 64 + +#define CP_COMPACT_SUM_FLAG 0x00000004 +#define CP_UMOUNT_FLAG 0x00000001 + +#define MAX_ACTIVE_LOGS 16 +#define MAX_ACTIVE_NODE_LOGS 8 +#define MAX_ACTIVE_DATA_LOGS 8 +#define NR_CURSEG_DATA_TYPE 3 +#define NR_CURSEG_NODE_TYPE 3 +#define NR_CURSEG_TYPE (NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE) + +#define ENTRIES_IN_SUM 512 +#define SUMMARY_SIZE 7 +#define SUM_FOOTER_SIZE 5 +#define JENTRY_SIZE (sizeof(struct grub_f2fs_nat_jent)) +#define SUM_ENTRIES_SIZE (SUMMARY_SIZE * ENTRIES_IN_SUM) +#define SUM_JOURNAL_SIZE (F2FS_BLKSIZE - SUM_FOOTER_SIZE -\ + SUM_ENTRIES_SIZE) +#define NAT_JOURNAL_ENTRIES ((SUM_JOURNAL_SIZE - 2) / JENTRY_SIZE) +#define NAT_JOURNAL_RESERVED ((SUM_JOURNAL_SIZE - 2) % JENTRY_SIZE) + +#define NAT_ENTRY_SIZE (sizeof(struct grub_f2fs_nat_entry)) +#define NAT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / NAT_ENTRY_SIZE) + +#define F2FS_NAME_LEN 255 +#define F2FS_SLOT_LEN 8 +#define NR_DENTRY_IN_BLOCK 214 +#define SIZE_OF_DIR_ENTRY 11 /* by byte */ +#define BITS_PER_BYTE 8 +#define SIZE_OF_DENTRY_BITMAP ((NR_DENTRY_IN_BLOCK + BITS_PER_BYTE - 1) = / \ + BITS_PER_BYTE) +#define SIZE_OF_RESERVED (F2FS_BLKSIZE - ((SIZE_OF_DIR_ENTRY + \ + F2FS_SLOT_LEN) * \ + NR_DENTRY_IN_BLOCK + SIZE_OF_DENTRY_BITMAP)) + +#define F2FS_INLINE_XATTR_ADDRS 50 /* 200 bytes for inline xattrs */ +#define DEF_ADDRS_PER_INODE 923 /* Address Pointers in an Inode */ + +#define ADDRS_PER_BLOCK 1018 /* Address Pointers in a Direct Block */ +#define NIDS_PER_BLOCK 1018 /* Node IDs in an Indirect Block */ +#define NODE_DIR1_BLOCK (DEF_ADDRS_PER_INODE + 1) +#define NODE_DIR2_BLOCK (DEF_ADDRS_PER_INODE + 2) +#define NODE_IND1_BLOCK (DEF_ADDRS_PER_INODE + 3) +#define NODE_IND2_BLOCK (DEF_ADDRS_PER_INODE + 4) +#define NODE_DIND_BLOCK (DEF_ADDRS_PER_INODE + 5) + +#define MAX_INLINE_DATA (4 * (DEF_ADDRS_PER_INODE - \ + F2FS_INLINE_XATTR_ADDRS - 1)) +#define NR_INLINE_DENTRY (MAX_INLINE_DATA * BITS_PER_BYTE / \ + ((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \ + BITS_PER_BYTE + 1)) +#define INLINE_DENTRY_BITMAP_SIZE ((NR_INLINE_DENTRY + \ + BITS_PER_BYTE - 1) / BITS_PER_BYTE) +#define INLINE_RESERVED_SIZE (MAX_INLINE_DATA - \ + ((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \ + NR_INLINE_DENTRY + INLINE_DENTRY_BITMAP_SIZE)) +#define CURSEG_HOT_DATA 0 + +#define CKPT_FLAG_SET(ckpt, f) \ + (ckpt)->ckpt_flags & grub_cpu_to_le32_compile_time (f) + +#define F2FS_INLINE_XATTR 0x01 /* file inline xattr flag */ +#define F2FS_INLINE_DATA 0x02 /* file inline data flag */ +#define F2FS_INLINE_DENTRY 0x04 /* file inline dentry flag */ +#define F2FS_DATA_EXIST 0x08 /* file inline data exist flag */ +#define F2FS_INLINE_DOTS 0x10 /* file having implicit dot dentries */ + +enum FILE_TYPE +{ + F2FS_FT_UNKNOWN, + F2FS_FT_REG_FILE =3D 1, + F2FS_FT_DIR =3D 2, + F2FS_FT_SYMLINK =3D 7, +}; + +#define MAX_VOLUME_NAME 512 + +struct grub_f2fs_superblock +{ + grub_uint32_t magic; + grub_uint16_t dummy1[2]; + grub_uint32_t log_sectorsize; + grub_uint32_t log_sectors_per_block; + grub_uint32_t log_blocksize; + grub_uint32_t log_blocks_per_seg; + grub_uint32_t segs_per_sec; + grub_uint32_t secs_per_zone; + grub_uint32_t checksum_offset; + grub_uint8_t dummy2[40]; + grub_uint32_t cp_blkaddr; + grub_uint32_t sit_blkaddr; + grub_uint32_t nat_blkaddr; + grub_uint32_t ssa_blkaddr; + grub_uint32_t main_blkaddr; + grub_uint32_t root_ino; + grub_uint32_t node_ino; + grub_uint32_t meta_ino; + grub_uint8_t uuid[16]; + grub_uint16_t volume_name[MAX_VOLUME_NAME]; + grub_uint32_t extension_count; + grub_uint8_t extension_list[F2FS_MAX_EXTENSION][8]; + grub_uint32_t cp_payload; + grub_uint8_t version[VERSION_LEN]; + grub_uint8_t init_version[VERSION_LEN]; +} GRUB_PACKED; + +struct grub_f2fs_checkpoint +{ + grub_uint64_t checkpoint_ver; + grub_uint64_t user_block_count; + grub_uint64_t valid_block_count; + grub_uint32_t rsvd_segment_count; + grub_uint32_t overprov_segment_count; + grub_uint32_t free_segment_count; + grub_uint32_t cur_node_segno[MAX_ACTIVE_NODE_LOGS]; + grub_uint16_t cur_node_blkoff[MAX_ACTIVE_NODE_LOGS]; + grub_uint32_t cur_data_segno[MAX_ACTIVE_DATA_LOGS]; + grub_uint16_t cur_data_blkoff[MAX_ACTIVE_DATA_LOGS]; + grub_uint32_t ckpt_flags; + grub_uint32_t cp_pack_total_block_count; + grub_uint32_t cp_pack_start_sum; + grub_uint32_t valid_node_count; + grub_uint32_t valid_inode_count; + grub_uint32_t next_free_nid; + grub_uint32_t sit_ver_bitmap_bytesize; + grub_uint32_t nat_ver_bitmap_bytesize; + grub_uint32_t checksum_offset; + grub_uint64_t elapsed_time; + grub_uint8_t alloc_type[MAX_ACTIVE_LOGS]; + grub_uint8_t sit_nat_version_bitmap[3900]; + grub_uint32_t checksum; +} GRUB_PACKED; + +struct grub_f2fs_nat_entry { + grub_uint8_t version; + grub_uint32_t ino; + grub_uint32_t block_addr; +} GRUB_PACKED; + +struct grub_f2fs_nat_jent +{ + grub_uint32_t nid; + struct grub_f2fs_nat_entry ne; +} GRUB_PACKED; + +struct grub_f2fs_nat_journal { + grub_uint16_t n_nats; + struct grub_f2fs_nat_jent entries[NAT_JOURNAL_ENTRIES]; + grub_uint8_t reserved[NAT_JOURNAL_RESERVED]; +} GRUB_PACKED; + +struct grub_f2fs_nat_block { + struct grub_f2fs_nat_entry ne[NAT_ENTRY_PER_BLOCK]; +} GRUB_PACKED; + +struct grub_f2fs_dir_entry +{ + grub_uint32_t hash_code; + grub_uint32_t ino; + grub_uint16_t name_len; + grub_uint8_t file_type; +} GRUB_PACKED; + +struct grub_f2fs_inline_dentry +{ + grub_uint8_t dentry_bitmap[INLINE_DENTRY_BITMAP_SIZE]; + grub_uint8_t reserved[INLINE_RESERVED_SIZE]; + struct grub_f2fs_dir_entry dentry[NR_INLINE_DENTRY]; + grub_uint8_t filename[NR_INLINE_DENTRY][F2FS_SLOT_LEN]; +} GRUB_PACKED; + +struct grub_f2fs_dentry_block { + grub_uint8_t dentry_bitmap[SIZE_OF_DENTRY_BITMAP]; + grub_uint8_t reserved[SIZE_OF_RESERVED]; + struct grub_f2fs_dir_entry dentry[NR_DENTRY_IN_BLOCK]; + grub_uint8_t filename[NR_DENTRY_IN_BLOCK][F2FS_SLOT_LEN]; +} GRUB_PACKED; + +struct grub_f2fs_inode +{ + grub_uint16_t i_mode; + grub_uint8_t i_advise; + grub_uint8_t i_inline; + grub_uint32_t i_uid; + grub_uint32_t i_gid; + grub_uint32_t i_links; + grub_uint64_t i_size; + grub_uint64_t i_blocks; + grub_uint64_t i_atime; + grub_uint64_t i_ctime; + grub_uint64_t i_mtime; + grub_uint32_t i_atime_nsec; + grub_uint32_t i_ctime_nsec; + grub_uint32_t i_mtime_nsec; + grub_uint32_t i_generation; + grub_uint32_t i_current_depth; + grub_uint32_t i_xattr_nid; + grub_uint32_t i_flags; + grub_uint32_t i_pino; + grub_uint32_t i_namelen; + grub_uint8_t i_name[F2FS_NAME_LEN]; + grub_uint8_t i_dir_level; + grub_uint8_t i_ext[12]; + grub_uint32_t i_addr[DEF_ADDRS_PER_INODE]; + grub_uint32_t i_nid[5]; +} GRUB_PACKED; + +struct grub_direct_node { + grub_uint32_t addr[ADDRS_PER_BLOCK]; +} GRUB_PACKED; + +struct grub_indirect_node { + grub_uint32_t nid[NIDS_PER_BLOCK]; +} GRUB_PACKED; + +struct grub_f2fs_node +{ + union + { + struct grub_f2fs_inode i; + struct grub_direct_node dn; + struct grub_indirect_node in; + char buf[F2FS_BLKSIZE - 40]; /* Should occupy F2FS_BLKSIZE totally *= / + }; + grub_uint8_t dummy[40]; +} GRUB_PACKED; + +struct grub_fshelp_node +{ + struct grub_f2fs_data *data; + struct grub_f2fs_node inode; + grub_uint32_t ino; + int inode_read; +}; + +struct grub_f2fs_data +{ + struct grub_f2fs_superblock sblock; + struct grub_f2fs_checkpoint ckpt; + + grub_uint32_t root_ino; + grub_uint32_t blocks_per_seg; + grub_uint32_t cp_blkaddr; + grub_uint32_t nat_blkaddr; + + struct grub_f2fs_nat_journal nat_j; + char *nat_bitmap; + + grub_disk_t disk; + struct grub_f2fs_node *inode; + struct grub_fshelp_node diropen; +}; + +struct grub_f2fs_dir_iter_ctx +{ + struct grub_f2fs_data *data; + grub_fshelp_iterate_dir_hook_t hook; + void *hook_data; + grub_uint8_t *bitmap; + grub_uint8_t (*filename)[F2FS_SLOT_LEN]; + struct grub_f2fs_dir_entry *dentry; + int max; +}; + +struct grub_f2fs_dir_ctx +{ + grub_fs_dir_hook_t hook; + void *hook_data; + struct grub_f2fs_data *data; +}; + +static grub_dl_t my_mod; + +static inline int +grub_f2fs_test_bit_le (int nr, const grub_uint8_t *addr) +{ + return addr[nr >> 3] & (1 << (nr & 7)); +} + +static inline char * +__inline_addr (struct grub_f2fs_inode *inode) +{ + return (char *)&inode->i_addr[1]; +} + +static inline grub_uint64_t +grub_f2fs_file_size (struct grub_f2fs_inode *inode) +{ + return grub_le_to_cpu64 (inode->i_size); +} + +static inline grub_uint32_t +__start_cp_addr (struct grub_f2fs_data *data) +{ + struct grub_f2fs_checkpoint *ckpt =3D &data->ckpt; + grub_uint32_t start_addr =3D data->cp_blkaddr; + + if (!(ckpt->checkpoint_ver & grub_cpu_to_le64_compile_time(1))) + return start_addr + data->blocks_per_seg; + return start_addr; +} + +static inline grub_uint32_t +__start_sum_block (struct grub_f2fs_data *data) +{ + struct grub_f2fs_checkpoint *ckpt =3D &data->ckpt; + + return __start_cp_addr (data) + grub_le_to_cpu32 (ckpt->cp_pack_start_= sum); +} + +static inline grub_uint32_t +__sum_blk_addr (struct grub_f2fs_data *data, int base, int type) +{ + struct grub_f2fs_checkpoint *ckpt =3D &data->ckpt; + + return __start_cp_addr (data) + + grub_le_to_cpu32 (ckpt->cp_pack_total_block_count) + - (base + 1) + type; +} + +static inline void * +__nat_bitmap_ptr (struct grub_f2fs_data *data) +{ + struct grub_f2fs_checkpoint *ckpt =3D &data->ckpt; + grub_uint32_t offset; + + if (grub_le_to_cpu32 (data->sblock.cp_payload) > 0) + return ckpt->sit_nat_version_bitmap; + + offset =3D grub_le_to_cpu32 (ckpt->sit_ver_bitmap_bytesize); + return ckpt->sit_nat_version_bitmap + offset; +} + +static inline grub_uint32_t +__get_node_id (struct grub_f2fs_node *rn, int off, int inode_block) +{ + if (inode_block) + return grub_le_to_cpu32 (rn->i.i_nid[off - NODE_DIR1_BLOCK]); + return grub_le_to_cpu32 (rn->in.nid[off]); +} + +static inline grub_err_t +grub_f2fs_block_read (struct grub_f2fs_data *data, grub_uint32_t blkaddr= , void *buf) +{ + return grub_disk_read (data->disk, + ((grub_disk_addr_t)blkaddr) << F2FS_BLK_SEC_BITS, + 0, F2FS_BLKSIZE, buf); +} + +/* + * CRC32 +*/ +#define CRCPOLY_LE 0xedb88320 + +static inline grub_uint32_t +grub_f2fs_cal_crc32 (const void *buf, const grub_uint32_t len) +{ + grub_uint32_t crc =3D F2FS_SUPER_MAGIC; + unsigned char *p =3D (unsigned char *)buf; + grub_uint32_t tmp =3D len; + int i; + + while (tmp--) + { + crc ^=3D *p++; + for (i =3D 0; i < 8; i++) + crc =3D (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0); + } + return crc; +} + +static inline int +grub_f2fs_crc_valid (grub_uint32_t blk_crc, void *buf, const grub_uint32= _t len) +{ + grub_uint32_t cal_crc =3D 0; + + cal_crc =3D grub_f2fs_cal_crc32 (buf, len); + + return (cal_crc =3D=3D blk_crc) ? 1 : 0; +} + +static inline int +grub_f2fs_test_bit (grub_uint32_t nr, const char *p) +{ + int mask; + + p +=3D (nr >> 3); + mask =3D 1 << (7 - (nr & 0x07)); + return mask & *p; +} + +static int +grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb) +{ + grub_uint32_t log_sectorsize, log_sectors_per_block; + + if (sb->magic !=3D grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC)) + return -1; + + if (sb->log_blocksize !=3D grub_cpu_to_le32_compile_time (F2FS_BLK_BIT= S)) + return -1; + + log_sectorsize =3D grub_le_to_cpu32 (sb->log_sectorsize); + log_sectors_per_block =3D grub_le_to_cpu32 (sb->log_sectors_per_block)= ; + + if (log_sectorsize > F2FS_BLK_BITS) + return -1; + + if (log_sectorsize < F2FS_MIN_LOG_SECTOR_SIZE) + return -1; + + if (log_sectors_per_block + log_sectorsize !=3D F2FS_BLK_BITS) + return -1; + + return 0; +} + +static int +grub_f2fs_read_sb (struct grub_f2fs_data *data, grub_disk_addr_t offset) +{ + grub_disk_t disk =3D data->disk; + grub_err_t err; + + /* Read first super block. */ + err =3D grub_disk_read (disk, offset, 0, sizeof (data->sblock), &data-= >sblock); + if (err) + return -1; + + return grub_f2fs_sanity_check_sb (&data->sblock); +} + +static void * +validate_checkpoint (struct grub_f2fs_data *data, grub_uint32_t cp_addr, + grub_uint64_t *version) +{ + grub_uint32_t *cp_page_1, *cp_page_2; + struct grub_f2fs_checkpoint *cp_block; + grub_uint64_t cur_version =3D 0, pre_version =3D 0; + grub_uint32_t crc =3D 0; + grub_uint32_t crc_offset; + grub_err_t err; + + /* Read the 1st cp block in this CP pack */ + cp_page_1 =3D grub_malloc (F2FS_BLKSIZE); + if (!cp_page_1) + return NULL; + + err =3D grub_f2fs_block_read (data, cp_addr, cp_page_1); + if (err) + goto invalid_cp1; + + cp_block =3D (struct grub_f2fs_checkpoint *)cp_page_1; + crc_offset =3D grub_le_to_cpu32 (cp_block->checksum_offset); + if (crc_offset !=3D CHECKSUM_OFFSET) + goto invalid_cp1; + + crc =3D grub_le_to_cpu32 (*(cp_page_1 + U32_CHECKSUM_OFFSET)); + if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset)) + goto invalid_cp1; + + pre_version =3D grub_le_to_cpu64 (cp_block->checkpoint_ver); + + /* Read the 2nd cp block in this CP pack */ + cp_page_2 =3D grub_malloc (F2FS_BLKSIZE); + if (!cp_page_2) + goto invalid_cp1; + + cp_addr +=3D grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - = 1; + + err =3D grub_f2fs_block_read (data, cp_addr, cp_page_2); + if (err) + goto invalid_cp2; + + cp_block =3D (struct grub_f2fs_checkpoint *)cp_page_2; + crc_offset =3D grub_le_to_cpu32 (cp_block->checksum_offset); + if (crc_offset !=3D CHECKSUM_OFFSET) + goto invalid_cp2; + + crc =3D grub_le_to_cpu32 (*(cp_page_2 + U32_CHECKSUM_OFFSET)); + if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset)) + goto invalid_cp2; + + cur_version =3D grub_le_to_cpu64 (cp_block->checkpoint_ver); + if (cur_version =3D=3D pre_version) + { + *version =3D cur_version; + grub_free (cp_page_2); + return cp_page_1; + } + +invalid_cp2: + grub_free (cp_page_2); +invalid_cp1: + grub_free (cp_page_1); + return NULL; +} + +static grub_err_t +grub_f2fs_read_cp (struct grub_f2fs_data *data) +{ + void *cp1, *cp2, *cur_page; + grub_uint64_t cp1_version =3D 0, cp2_version =3D 0; + grub_uint64_t cp_start_blk_no; + + /* + * Finding out valid cp block involves read both + * sets (cp pack1 and cp pack 2) + */ + cp_start_blk_no =3D data->cp_blkaddr; + cp1 =3D validate_checkpoint (data, cp_start_blk_no, &cp1_version); + if (!cp1 && grub_errno) + return grub_errno; + + /* The second checkpoint pack should start at the next segment */ + cp_start_blk_no +=3D data->blocks_per_seg; + cp2 =3D validate_checkpoint (data, cp_start_blk_no, &cp2_version); + if (!cp2 && grub_errno) + { + grub_free (cp1); + return grub_errno; + } + + if (cp1 && cp2) + cur_page =3D (cp2_version > cp1_version) ? cp2 : cp1; + else if (cp1) + cur_page =3D cp1; + else if (cp2) + cur_page =3D cp2; + else + return grub_error (GRUB_ERR_BAD_FS, "no checkpoints"); + + grub_memcpy (&data->ckpt, cur_page, F2FS_BLKSIZE); + + grub_free (cp1); + grub_free (cp2); + return 0; +} + +static grub_err_t +get_nat_journal (struct grub_f2fs_data *data) +{ + grub_uint32_t block; + char *buf; + grub_err_t err; + + buf =3D grub_malloc (F2FS_BLKSIZE); + if (!buf) + return grub_errno; + + if (CKPT_FLAG_SET(&data->ckpt, CP_COMPACT_SUM_FLAG)) + block =3D __start_sum_block (data); + else if (CKPT_FLAG_SET (&data->ckpt, CP_UMOUNT_FLAG)) + block =3D __sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA); + else + block =3D __sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA= ); + + err =3D grub_f2fs_block_read (data, block, buf); + if (err) + goto fail; + + if (CKPT_FLAG_SET (&data->ckpt, CP_COMPACT_SUM_FLAG)) + grub_memcpy (&data->nat_j, buf, SUM_JOURNAL_SIZE); + else + grub_memcpy (&data->nat_j, buf + SUM_ENTRIES_SIZE, SUM_JOURNAL_SIZE)= ; + +fail: + grub_free (buf); + return err; +} + +static grub_uint32_t +get_blkaddr_from_nat_journal (struct grub_f2fs_data *data, grub_uint32_t= nid) +{ + grub_uint16_t n =3D grub_le_to_cpu16 (data->nat_j.n_nats); + grub_uint32_t blkaddr =3D 0; + grub_uint16_t i; + + for (i =3D 0; i < n; i++) + { + if (grub_le_to_cpu32 (data->nat_j.entries[i].nid) =3D=3D nid) + { + blkaddr =3D grub_le_to_cpu32 (data->nat_j.entries[i].ne.block_= addr); + break; + } + } + return blkaddr; +} + +static grub_uint32_t +get_node_blkaddr (struct grub_f2fs_data *data, grub_uint32_t nid) +{ + struct grub_f2fs_nat_block *nat_block; + grub_uint32_t seg_off, block_off, entry_off, block_addr; + grub_uint32_t blkaddr; + grub_err_t err; + + blkaddr =3D get_blkaddr_from_nat_journal (data, nid); + if (blkaddr) + return blkaddr; + + nat_block =3D grub_malloc (F2FS_BLKSIZE); + if (!nat_block) + return 0; + + block_off =3D nid / NAT_ENTRY_PER_BLOCK; + entry_off =3D nid % NAT_ENTRY_PER_BLOCK; + + seg_off =3D block_off / data->blocks_per_seg; + block_addr =3D data->nat_blkaddr + + ((seg_off * data->blocks_per_seg) << 1) + + (block_off & (data->blocks_per_seg - 1)); + + if (grub_f2fs_test_bit (block_off, data->nat_bitmap)) + block_addr +=3D data->blocks_per_seg; + + err =3D grub_f2fs_block_read (data, block_addr, nat_block); + if (err) + { + grub_free (nat_block); + return 0; + } + + blkaddr =3D grub_le_to_cpu32 (nat_block->ne[entry_off].block_addr); + + grub_free (nat_block); + + return blkaddr; +} + +static int +grub_get_node_path (struct grub_f2fs_inode *inode, grub_uint32_t block, + grub_uint32_t offset[4], grub_uint32_t noffset[4]) +{ + grub_uint32_t direct_blks =3D ADDRS_PER_BLOCK; + grub_uint32_t dptrs_per_blk =3D NIDS_PER_BLOCK; + grub_uint32_t indirect_blks =3D ADDRS_PER_BLOCK * NIDS_PER_BLOCK; + grub_uint32_t dindirect_blks =3D indirect_blks * NIDS_PER_BLOCK; + grub_uint32_t direct_index =3D DEF_ADDRS_PER_INODE; + int n =3D 0; + int level =3D 0; + + if (inode->i_inline & F2FS_INLINE_XATTR) + direct_index -=3D F2FS_INLINE_XATTR_ADDRS; + + noffset[0] =3D 0; + + if (block < direct_index) + { + offset[n] =3D block; + goto got; + } + + block -=3D direct_index; + if (block < direct_blks) + { + offset[n++] =3D NODE_DIR1_BLOCK; + noffset[n] =3D 1; + offset[n] =3D block; + level =3D 1; + goto got; + } + + block -=3D direct_blks; + if (block < direct_blks) + { + offset[n++] =3D NODE_DIR2_BLOCK; + noffset[n] =3D 2; + offset[n] =3D block; + level =3D 1; + goto got; + } + + block -=3D direct_blks; + if (block < indirect_blks) + { + offset[n++] =3D NODE_IND1_BLOCK; + noffset[n] =3D 3; + offset[n++] =3D block / direct_blks; + noffset[n] =3D 4 + offset[n - 1]; + offset[n] =3D block % direct_blks; + level =3D 2; + goto got; + } + + block -=3D indirect_blks; + if (block < indirect_blks) + { + offset[n++] =3D NODE_IND2_BLOCK; + noffset[n] =3D 4 + dptrs_per_blk; + offset[n++] =3D block / direct_blks; + noffset[n] =3D 5 + dptrs_per_blk + offset[n - 1]; + offset[n] =3D block % direct_blks; + level =3D 2; + goto got; + } + + block -=3D indirect_blks; + if (block < dindirect_blks) + { + offset[n++] =3D NODE_DIND_BLOCK; + noffset[n] =3D 5 + (dptrs_per_blk * 2); + offset[n++] =3D block / indirect_blks; + noffset[n] =3D 6 + (dptrs_per_blk * 2) + + offset[n - 1] * (dptrs_per_blk + 1); + offset[n++] =3D (block / direct_blks) % dptrs_per_blk; + noffset[n] =3D 7 + (dptrs_per_blk * 2) + + offset[n - 2] * (dptrs_per_blk + 1) + offset[n - 1]; + offset[n] =3D block % direct_blks; + level =3D 3; + goto got; + } +got: + return level; +} + +static grub_err_t +grub_f2fs_read_node (struct grub_f2fs_data *data, + grub_uint32_t nid, struct grub_f2fs_node *np) +{ + grub_uint32_t blkaddr; + + blkaddr =3D get_node_blkaddr (data, nid); + if (!blkaddr) + return grub_errno; + + return grub_f2fs_block_read (data, blkaddr, np); +} + +static struct grub_f2fs_data * +grub_f2fs_mount (grub_disk_t disk) +{ + struct grub_f2fs_data *data; + grub_err_t err; + + data =3D grub_malloc (sizeof (*data)); + if (!data) + return NULL; + + data->disk =3D disk; + + if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET0)) + { + if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET1)) + { + if (grub_errno =3D=3D GRUB_ERR_NONE) + grub_error (GRUB_ERR_BAD_FS, + "not a F2FS filesystem (no superblock)"); + goto fail; + } + } + + data->root_ino =3D grub_le_to_cpu32 (data->sblock.root_ino); + data->cp_blkaddr =3D grub_le_to_cpu32 (data->sblock.cp_blkaddr); + data->nat_blkaddr =3D grub_le_to_cpu32 (data->sblock.nat_blkaddr); + data->blocks_per_seg =3D 1 << + grub_le_to_cpu32 (data->sblock.log_blocks_per_seg); + + err =3D grub_f2fs_read_cp (data); + if (err) + goto fail; + + data->nat_bitmap =3D __nat_bitmap_ptr (data); + + err =3D get_nat_journal (data); + if (err) + goto fail; + + data->diropen.data =3D data; + data->diropen.ino =3D data->root_ino; + data->diropen.inode_read =3D 1; + data->inode =3D &data->diropen.inode; + + err =3D grub_f2fs_read_node (data, data->root_ino, data->inode); + if (err) + goto fail; + + return data; + +fail: + grub_free (data); + return NULL; +} + +/* guarantee inline_data was handled by caller */ +static grub_disk_addr_t +grub_f2fs_get_block (grub_fshelp_node_t node, grub_disk_addr_t block_ofs= ) +{ + struct grub_f2fs_data *data =3D node->data; + struct grub_f2fs_inode *inode =3D &node->inode.i; + grub_uint32_t offset[4], noffset[4], nids[4]; + struct grub_f2fs_node *node_block; + grub_uint32_t block_addr =3D -1; + int level, i; + + level =3D grub_get_node_path (inode, block_ofs, offset, noffset); + if (level =3D=3D 0) + return grub_le_to_cpu32 (inode->i_addr[offset[0]]); + + node_block =3D grub_malloc (F2FS_BLKSIZE); + if (!node_block) + return -1; + + nids[1] =3D __get_node_id (&node->inode, offset[0], 1); + + /* get indirect or direct nodes */ + for (i =3D 1; i <=3D level; i++) + { + grub_f2fs_read_node (data, nids[i], node_block); + if (grub_errno) + goto fail; + + if (i < level) + nids[i + 1] =3D __get_node_id (node_block, offset[i], 0); + } + + block_addr =3D grub_le_to_cpu32 (node_block->dn.addr[offset[level]]); +fail: + grub_free (node_block); + return block_addr; +} + +static grub_ssize_t +grub_f2fs_read_file (grub_fshelp_node_t node, + grub_disk_read_hook_t read_hook, void *read_hook_data, + grub_off_t pos, grub_size_t len, char *buf) +{ + struct grub_f2fs_inode *inode =3D &node->inode.i; + grub_off_t filesize =3D grub_f2fs_file_size (inode); + char *inline_addr =3D __inline_addr (inode); + + if (inode->i_inline & F2FS_INLINE_DATA) + { + if (filesize > MAX_INLINE_DATA) + return -1; + if (len > filesize - pos) + len =3D filesize - pos; + + grub_memcpy (buf, inline_addr + pos, len); + return len; + } + + return grub_fshelp_read_file (node->data->disk, node, + read_hook, read_hook_data, + pos, len, buf, grub_f2fs_get_block, + filesize, + F2FS_BLK_SEC_BITS, 0); +} + +static char * +grub_f2fs_read_symlink (grub_fshelp_node_t node) +{ + char *symlink; + struct grub_fshelp_node *diro =3D node; + grub_uint64_t filesize; + + if (!diro->inode_read) + { + grub_f2fs_read_node (diro->data, diro->ino, &diro->inode); + if (grub_errno) + return 0; + } + + filesize =3D grub_f2fs_file_size(&diro->inode.i); + + symlink =3D grub_malloc (filesize + 1); + if (!symlink) + return 0; + + grub_f2fs_read_file (diro, 0, 0, 0, filesize, symlink); + if (grub_errno) + { + grub_free (symlink); + return 0; + } + + symlink[filesize] =3D '\0'; + return symlink; +} + +static int +grub_f2fs_check_dentries (struct grub_f2fs_dir_iter_ctx *ctx) +{ + struct grub_fshelp_node *fdiro; + int i; + + for (i =3D 0; i < ctx->max;) + { + char *filename; + enum grub_fshelp_filetype type =3D GRUB_FSHELP_UNKNOWN; + enum FILE_TYPE ftype; + int name_len; + int ret; + + if (grub_f2fs_test_bit_le (i, ctx->bitmap) =3D=3D 0) + { + i++; + continue; + } + + ftype =3D ctx->dentry[i].file_type; + name_len =3D grub_le_to_cpu16 (ctx->dentry[i].name_len); + filename =3D grub_malloc (name_len + 1); + if (!filename) + return 0; + + grub_memcpy (filename, ctx->filename[i], name_len); + filename[name_len] =3D 0; + + fdiro =3D grub_malloc (sizeof (struct grub_fshelp_node)); + if (!fdiro) + { + grub_free(filename); + return 0; + } + + if (ftype =3D=3D F2FS_FT_DIR) + type =3D GRUB_FSHELP_DIR; + else if (ftype =3D=3D F2FS_FT_SYMLINK) + type =3D GRUB_FSHELP_SYMLINK; + else if (ftype =3D=3D F2FS_FT_REG_FILE) + type =3D GRUB_FSHELP_REG; + + fdiro->data =3D ctx->data; + fdiro->ino =3D grub_le_to_cpu32 (ctx->dentry[i].ino); + fdiro->inode_read =3D 0; + + ret =3D ctx->hook (filename, type, fdiro, ctx->hook_data); + grub_free(filename); + if (ret) + return 1; + + i +=3D (name_len + F2FS_SLOT_LEN - 1) / F2FS_SLOT_LEN; + } + return 0; +} + +static int +grub_f2fs_iterate_inline_dir (struct grub_f2fs_inode *dir, + struct grub_f2fs_dir_iter_ctx *ctx) +{ + struct grub_f2fs_inline_dentry *de_blk; + + de_blk =3D (struct grub_f2fs_inline_dentry *) __inline_addr (dir); + + ctx->bitmap =3D de_blk->dentry_bitmap; + ctx->dentry =3D de_blk->dentry; + ctx->filename =3D de_blk->filename; + ctx->max =3D NR_INLINE_DENTRY; + + return grub_f2fs_check_dentries (ctx); +} + +static int +grub_f2fs_iterate_dir (grub_fshelp_node_t dir, + grub_fshelp_iterate_dir_hook_t hook, void *hook_data) +{ + struct grub_fshelp_node *diro =3D (struct grub_fshelp_node *) dir; + struct grub_f2fs_inode *inode; + struct grub_f2fs_dir_iter_ctx ctx =3D { + .data =3D diro->data, + .hook =3D hook, + .hook_data =3D hook_data + }; + grub_off_t fpos =3D 0; + + if (!diro->inode_read) + { + grub_f2fs_read_node (diro->data, diro->ino, &diro->inode); + if (grub_errno) + return 0; + } + + inode =3D &diro->inode.i; + + if (inode->i_inline & F2FS_INLINE_DENTRY) + return grub_f2fs_iterate_inline_dir (inode, &ctx); + + while (fpos < grub_f2fs_file_size (inode)) + { + struct grub_f2fs_dentry_block *de_blk; + char *buf; + int ret; + + buf =3D grub_zalloc (F2FS_BLKSIZE); + if (!buf) + return 0; + + grub_f2fs_read_file (diro, 0, 0, fpos, F2FS_BLKSIZE, buf); + if (grub_errno) + { + grub_free (buf); + return 0; + } + + de_blk =3D (struct grub_f2fs_dentry_block *) buf; + + ctx.bitmap =3D de_blk->dentry_bitmap; + ctx.dentry =3D de_blk->dentry; + ctx.filename =3D de_blk->filename; + ctx.max =3D NR_DENTRY_IN_BLOCK; + + ret =3D grub_f2fs_check_dentries (&ctx); + grub_free (buf); + if (ret) + return 1; + + fpos +=3D F2FS_BLKSIZE; + } + return 0; +} + +static int +grub_f2fs_dir_iter (const char *filename, enum grub_fshelp_filetype file= type, + grub_fshelp_node_t node, void *data) +{ + struct grub_f2fs_dir_ctx *ctx =3D data; + struct grub_dirhook_info info; + + grub_memset (&info, 0, sizeof (info)); + if (!node->inode_read) + { + grub_f2fs_read_node (ctx->data, node->ino, &node->inode); + if (!grub_errno) + node->inode_read =3D 1; + grub_errno =3D GRUB_ERR_NONE; + } + if (node->inode_read) + { + info.mtimeset =3D 1; + info.mtime =3D grub_le_to_cpu64 (node->inode.i.i_mtime); + } + + info.dir =3D ((filetype & GRUB_FSHELP_TYPE_MASK) =3D=3D GRUB_FSHELP_DI= R); + grub_free (node); + return ctx->hook (filename, &info, ctx->hook_data); +} + +static grub_err_t +grub_f2fs_dir (grub_device_t device, const char *path, + grub_fs_dir_hook_t hook, void *hook_data) +{ + struct grub_f2fs_dir_ctx ctx =3D { + .hook =3D hook, + .hook_data =3D hook_data + }; + struct grub_fshelp_node *fdiro =3D 0; + + grub_dl_ref (my_mod); + + ctx.data =3D grub_f2fs_mount (device->disk); + if (!ctx.data) + goto fail; + + grub_fshelp_find_file (path, &ctx.data->diropen, &fdiro, + grub_f2fs_iterate_dir, grub_f2fs_read_symlink, + GRUB_FSHELP_DIR); + if (grub_errno) + goto fail; + + grub_f2fs_iterate_dir (fdiro, grub_f2fs_dir_iter, &ctx); + +fail: + if (fdiro !=3D &ctx.data->diropen) + grub_free (fdiro); + grub_free (ctx.data); + grub_dl_unref (my_mod); + return grub_errno; +} + + +/* Open a file named NAME and initialize FILE. */ +static grub_err_t +grub_f2fs_open (struct grub_file *file, const char *name) +{ + struct grub_f2fs_data *data =3D NULL; + struct grub_fshelp_node *fdiro =3D 0; + struct grub_f2fs_inode *inode; + + grub_dl_ref (my_mod); + + data =3D grub_f2fs_mount (file->device->disk); + if (!data) + goto fail; + + grub_fshelp_find_file (name, &data->diropen, &fdiro, + grub_f2fs_iterate_dir, grub_f2fs_read_symlink, + GRUB_FSHELP_REG); + if (grub_errno) + goto fail; + + if (!fdiro->inode_read) + { + grub_f2fs_read_node (data, fdiro->ino, &fdiro->inode); + if (grub_errno) + goto fail; + } + + grub_memcpy (data->inode, &fdiro->inode, sizeof (*data->inode)); + grub_free (fdiro); + + inode =3D &(data->inode->i); + file->size =3D grub_f2fs_file_size (inode); + file->data =3D data; + file->offset =3D 0; + + if (inode->i_inline & F2FS_INLINE_DATA && file->size > MAX_INLINE_DATA= ) + grub_error (GRUB_ERR_BAD_FS, "corrupted inline_data: need fsck"); + return 0; + +fail: + if (fdiro !=3D &data->diropen) + grub_free (fdiro); + grub_free (data); + + grub_dl_unref (my_mod); + + return grub_errno; +} + +static grub_ssize_t +grub_f2fs_read (grub_file_t file, char *buf, grub_size_t len) +{ + struct grub_f2fs_data *data =3D (struct grub_f2fs_data *) file->data; + + return grub_f2fs_read_file (&data->diropen, + file->read_hook, file->read_hook_data, + file->offset, len, buf); +} + +static grub_err_t +grub_f2fs_close (grub_file_t file) +{ + struct grub_f2fs_data *data =3D (struct grub_f2fs_data *) file->data; + + grub_free (data); + + grub_dl_unref (my_mod); + + return GRUB_ERR_NONE; +} + +static grub_uint8_t * +grub_f2fs_utf16_to_utf8 (grub_uint16_t *in_buf_le) +{ + grub_uint16_t in_buf[MAX_VOLUME_NAME]; + grub_uint8_t *out_buf; + int len =3D 0; + + out_buf =3D grub_malloc (MAX_VOLUME_NAME * GRUB_MAX_UTF8_PER_UTF16 + 1= ); + if (!out_buf) + return NULL; + + while (*in_buf_le !=3D 0 && len < MAX_VOLUME_NAME) { + in_buf[len] =3D grub_le_to_cpu16 (in_buf_le[len]); + len++; + } + + *grub_utf16_to_utf8 (out_buf, in_buf, len) =3D '\0'; + return out_buf; +} + +static grub_err_t +grub_f2fs_label (grub_device_t device, char **label) +{ + struct grub_f2fs_data *data; + grub_disk_t disk =3D device->disk; + + grub_dl_ref (my_mod); + + data =3D grub_f2fs_mount (disk); + if (data) + *label =3D (char *) grub_f2fs_utf16_to_utf8 (data->sblock.volume_nam= e); + else + *label =3D NULL; + + grub_free (data); + grub_dl_unref (my_mod); + return grub_errno; +} + +static grub_err_t +grub_f2fs_uuid (grub_device_t device, char **uuid) +{ + struct grub_f2fs_data *data; + grub_disk_t disk =3D device->disk; + + grub_dl_ref (my_mod); + + data =3D grub_f2fs_mount (disk); + if (data) + { + *uuid =3D + grub_xasprintf + ("%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x"= , + data->sblock.uuid[0], data->sblock.uuid[1], + data->sblock.uuid[2], data->sblock.uuid[3], + data->sblock.uuid[4], data->sblock.uuid[5], + data->sblock.uuid[6], data->sblock.uuid[7], + data->sblock.uuid[8], data->sblock.uuid[9], + data->sblock.uuid[10], data->sblock.uuid[11], + data->sblock.uuid[12], data->sblock.uuid[13], + data->sblock.uuid[14], data->sblock.uuid[15]); + } + else + *uuid =3D NULL; + + grub_free (data); + grub_dl_unref (my_mod); + return grub_errno; +} + +static struct grub_fs grub_f2fs_fs =3D { + .name =3D "f2fs", + .dir =3D grub_f2fs_dir, + .open =3D grub_f2fs_open, + .read =3D grub_f2fs_read, + .close =3D grub_f2fs_close, + .label =3D grub_f2fs_label, + .uuid =3D grub_f2fs_uuid, +#ifdef GRUB_UTIL + .reserved_first_sector =3D 1, + .blocklist_install =3D 0, +#endif + .next =3D 0 +}; + +GRUB_MOD_INIT (f2fs) +{ + grub_fs_register (&grub_f2fs_fs); + my_mod =3D mod; +} + +GRUB_MOD_FINI (f2fs) +{ + grub_fs_unregister (&grub_f2fs_fs); +} diff --git a/po/exclude.pot b/po/exclude.pot index 0a9b215ea..816089c30 100644 --- a/po/exclude.pot +++ b/po/exclude.pot @@ -1214,6 +1214,7 @@ msgstr "" =20 #: grub-core/commands/xnu_uuid.c:75 grub-core/fs/jfs.c:924 #: grub-core/fs/nilfs2.c:1135 +#: grub-core/fs/f2fs.c:1259 #, c-format msgid "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%= 02x" msgstr "" diff --git a/tests/f2fs_test.in b/tests/f2fs_test.in new file mode 100644 index 000000000..1ea77c826 --- /dev/null +++ b/tests/f2fs_test.in @@ -0,0 +1,19 @@ +#!/bin/sh + +set -e + +if [ "x$EUID" =3D "x" ] ; then + EUID=3D`id -u` +fi + +if [ "$EUID" !=3D 0 ] ; then + exit 77 +fi + +if ! which mkfs.f2fs >/dev/null 2>&1; then + echo "mkfs.f2fs not installed; cannot test f2fs." + exit 77 +fi + + +"@builddir@/grub-fs-tester" f2fs diff --git a/tests/util/grub-fs-tester.in b/tests/util/grub-fs-tester.in index 2337771a1..333c45035 100644 --- a/tests/util/grub-fs-tester.in +++ b/tests/util/grub-fs-tester.in @@ -145,7 +145,7 @@ for ((LOGSECSIZE=3DMINLOGSECSIZE;LOGSECSIZE<=3DMAXLOG= SECSIZE;LOGSECSIZE=3DLOGSECSIZE + xsquash*) MINBLKSIZE=3D4096 MAXBLKSIZE=3D1048576;; - xxfs) + xxfs|xf2fs) MINBLKSIZE=3D$SECSIZE # OS Limitation: GNU/Linux doesn't accept > 4096 MAXBLKSIZE=3D4096;; @@ -268,6 +268,10 @@ for ((LOGSECSIZE=3DMINLOGSECSIZE;LOGSECSIZE<=3DMAXLO= GSECSIZE;LOGSECSIZE=3DLOGSECSIZE + x"btrfs"*) FSLABEL=3D"grub_;/test=C3=A9=F0=AF=A6=9B=F0=AF=A6=9D=F0=9F=98=81=D0= =BA=D0=B8=D1=80=D0=B8=D1=82i urewfceniuewruevrewnuuireurevueurnievrewfner= fcnevirivinrewvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvew= nvrenurnunuvrevuurerejiremvreijnvcreivire nverivnreivrevnureiorfnfrvoeoir= oireoireoifrefoieroifoireoi";; =20 + # FS LIMITATION: f2fs label is at most 512 UTF-16 chars + x"f2fs") + FSLABEL=3D"grub_;/test=C3=A9=E4=8F=8C=E4=90=93=E4=8F=95=D0=BA=D0=B8= =D1=80=D0=B8=D1=82iurewfceniuewruewnuuireurevueurnievrewfnerfcnevirivinre= wvnirewnivrewiuvcrewvnuewvrrrewniuerwreiuviurewiuviurewnuvewnvrenurnunuvr= evuurerejiremvreijnvvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifr= efoieroifoirvcreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroi= foircreivire nverivnreivrevnureiorfnfrvoeoiroireoireoifrefoieroifoireoifo= iq";; + # FS LIMITATION: exfat is at most 15 UTF-16 chars x"exfat") FSLABEL=3D"g=C3=A9=D1=82 ;/=F0=AF=A6=9B=F0=AF=A6=9D=F0=9F=98=81=D0= =BA=D0=B8=D1=80";; @@ -477,7 +481,7 @@ for ((LOGSECSIZE=3DMINLOGSECSIZE;LOGSECSIZE<=3DMAXLOG= SECSIZE;LOGSECSIZE=3DLOGSECSIZE + # FIXME: Not sure about BtrFS, NTFS, JFS, AFS, UDF and SFS. Check i= t. # FS LIMITATION: as far as I know those FS don't store their last modif= ication date. x"jfs_caseins" | x"jfs" | x"xfs" | x"xfs_crc" | x"btrfs"* | x"reiserfs= _old" | x"reiserfs" \ - | x"bfs" | x"afs" \ + | x"bfs" | x"afs" | x"f2fs" \ | x"tarfs" | x"cpio_"* | x"minix" | x"minix2" \ | x"minix3" | x"ntfs"* | x"udf" | x"sfs"*) NOFSTIME=3Dy;; @@ -756,6 +760,8 @@ for ((LOGSECSIZE=3DMINLOGSECSIZE;LOGSECSIZE<=3DMAXLOG= SECSIZE;LOGSECSIZE=3DLOGSECSIZE + MOUNTDEVICE=3D"/dev/mapper/grub_test-testvol" MOUNTFS=3Dext2 "mkfs.ext2" -L "$FSLABEL" -q "${MOUNTDEVICE}" ;; + xf2fs) + "mkfs.f2fs" -l "$FSLABEL" -q "${LODEVICES[0]}" ;; xnilfs2) "mkfs.nilfs2" -L "$FSLABEL" -b $BLKSIZE -q "${LODEVICES[0]}" ;; xext2_old) --=20 2.11.0