diff --git a/osufs/.gitignore b/osufs/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..c545112057ddf11017534ed98cc531151a32c3f7 --- /dev/null +++ b/osufs/.gitignore @@ -0,0 +1 @@ +xcuserdata/ diff --git a/osufs/osufs.xcodeproj/project.pbxproj b/osufs/osufs.xcodeproj/project.pbxproj new file mode 100644 index 0000000000000000000000000000000000000000..db59c62112972010df62551813f09ad883fa696b --- /dev/null +++ b/osufs/osufs.xcodeproj/project.pbxproj @@ -0,0 +1,282 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 48; + objects = { + +/* Begin PBXBuildFile section */ + 2012336B1FCD4FC400FCBFC8 /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 201233691FCD4FC400FCBFC8 /* main.cpp */; }; + 2012336E1FCD501200FCBFC8 /* osufs.c in Sources */ = {isa = PBXBuildFile; fileRef = 2012336C1FCD501200FCBFC8 /* osufs.c */; }; + 201233711FCD581200FCBFC8 /* blkio.c in Sources */ = {isa = PBXBuildFile; fileRef = 2012336F1FCD581200FCBFC8 /* blkio.c */; }; +/* End PBXBuildFile section */ + +/* Begin PBXCopyFilesBuildPhase section */ + 2012335D1FCD4F4400FCBFC8 /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; +/* End PBXCopyFilesBuildPhase section */ + +/* Begin PBXFileReference section */ + 2012335F1FCD4F4400FCBFC8 /* osufs */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = osufs; sourceTree = BUILT_PRODUCTS_DIR; }; + 201233691FCD4FC400FCBFC8 /* main.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = main.cpp; sourceTree = "<group>"; }; + 2012336C1FCD501200FCBFC8 /* osufs.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = osufs.c; sourceTree = "<group>"; }; + 2012336D1FCD501200FCBFC8 /* osufs.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = osufs.h; sourceTree = "<group>"; }; + 2012336F1FCD581200FCBFC8 /* blkio.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = blkio.c; sourceTree = "<group>"; }; + 201233701FCD581200FCBFC8 /* blkio.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = blkio.h; sourceTree = "<group>"; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + 2012335C1FCD4F4400FCBFC8 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 201233561FCD4F4300FCBFC8 = { + isa = PBXGroup; + children = ( + 201233611FCD4F4400FCBFC8 /* osufs */, + 201233601FCD4F4400FCBFC8 /* Products */, + ); + sourceTree = "<group>"; + }; + 201233601FCD4F4400FCBFC8 /* Products */ = { + isa = PBXGroup; + children = ( + 2012335F1FCD4F4400FCBFC8 /* osufs */, + ); + name = Products; + sourceTree = "<group>"; + }; + 201233611FCD4F4400FCBFC8 /* osufs */ = { + isa = PBXGroup; + children = ( + 201233691FCD4FC400FCBFC8 /* main.cpp */, + 2012336C1FCD501200FCBFC8 /* osufs.c */, + 2012336D1FCD501200FCBFC8 /* osufs.h */, + 2012336F1FCD581200FCBFC8 /* blkio.c */, + 201233701FCD581200FCBFC8 /* blkio.h */, + ); + path = osufs; + sourceTree = "<group>"; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + 2012335E1FCD4F4400FCBFC8 /* osufs */ = { + isa = PBXNativeTarget; + buildConfigurationList = 201233661FCD4F4400FCBFC8 /* Build configuration list for PBXNativeTarget "osufs" */; + buildPhases = ( + 2012335B1FCD4F4400FCBFC8 /* Sources */, + 2012335C1FCD4F4400FCBFC8 /* Frameworks */, + 2012335D1FCD4F4400FCBFC8 /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = osufs; + productName = osufs; + productReference = 2012335F1FCD4F4400FCBFC8 /* osufs */; + productType = "com.apple.product-type.tool"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 201233571FCD4F4300FCBFC8 /* Project object */ = { + isa = PBXProject; + attributes = { + LastUpgradeCheck = 0910; + ORGANIZATIONNAME = "Fang Lu"; + TargetAttributes = { + 2012335E1FCD4F4400FCBFC8 = { + CreatedOnToolsVersion = 9.1; + ProvisioningStyle = Automatic; + }; + }; + }; + buildConfigurationList = 2012335A1FCD4F4300FCBFC8 /* Build configuration list for PBXProject "osufs" */; + compatibilityVersion = "Xcode 8.0"; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + ); + mainGroup = 201233561FCD4F4300FCBFC8; + productRefGroup = 201233601FCD4F4400FCBFC8 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 2012335E1FCD4F4400FCBFC8 /* osufs */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXSourcesBuildPhase section */ + 2012335B1FCD4F4400FCBFC8 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 201233711FCD581200FCBFC8 /* blkio.c in Sources */, + 2012336E1FCD501200FCBFC8 /* osufs.c in Sources */, + 2012336B1FCD4FC400FCBFC8 /* main.cpp in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + 201233641FCD4F4400FCBFC8 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++98"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "-"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.12; + MTL_ENABLE_DEBUG_INFO = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = macosx; + }; + name = Debug; + }; + 201233651FCD4F4400FCBFC8 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++98"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "-"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.12; + MTL_ENABLE_DEBUG_INFO = NO; + SDKROOT = macosx; + }; + name = Release; + }; + 201233671FCD4F4400FCBFC8 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Debug; + }; + 201233681FCD4F4400FCBFC8 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 2012335A1FCD4F4300FCBFC8 /* Build configuration list for PBXProject "osufs" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 201233641FCD4F4400FCBFC8 /* Debug */, + 201233651FCD4F4400FCBFC8 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 201233661FCD4F4400FCBFC8 /* Build configuration list for PBXNativeTarget "osufs" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 201233671FCD4F4400FCBFC8 /* Debug */, + 201233681FCD4F4400FCBFC8 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = 201233571FCD4F4300FCBFC8 /* Project object */; +} diff --git a/osufs/osufs.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/osufs/osufs.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000000000000000000000000000000000000..2d1534d9fcaf91484e049c9be6ed8a8f8c75cde7 --- /dev/null +++ b/osufs/osufs.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ +<?xml version="1.0" encoding="UTF-8"?> +<Workspace + version = "1.0"> + <FileRef + location = "self:osufs.xcodeproj"> + </FileRef> +</Workspace> diff --git a/osufs/osufs/blkio.c b/osufs/osufs/blkio.c new file mode 100644 index 0000000000000000000000000000000000000000..a7ba77be458df3221b15fccd2504d4199830c7e1 --- /dev/null +++ b/osufs/osufs/blkio.c @@ -0,0 +1,110 @@ +// +// blkio.c +// osufs +// +// Created by Fang Lu on 11/28/17. +// Copyright © 2017 Fang Lu. All rights reserved. +// + +#include "blkio.h" + +#include <stdio.h> +#include <stdlib.h> +#include <sys/mman.h> +#include <fcntl.h> +#include <unistd.h> +#include <errno.h> +#include <string.h> + +#ifdef _WIN32 + +#error "Win32 NOT supported" + +#elif __APPLE__ + +#include <sys/disk.h> + +uint64_t blkio_size(uint32_t dev) { + uint64_t sector_count = 0; + // Query the number of sectors on the disk + if (ioctl(dev, DKIOCGETBLOCKCOUNT, §or_count) == -1) { + fprintf(stderr, "Failed to get block count\n"); + return 0; + } + fprintf(stderr, "IOCTL: block count is %llu\n", sector_count); + + uint32_t sector_size = 0; + // Query the size of each sector + if (ioctl(dev, DKIOCGETBLOCKSIZE, §or_size) == -1) { + fprintf(stderr, "Failed to get block size\n"); + return 0; + } + fprintf(stderr, "IOCTL: block size is %u\n", sector_size); + + return sector_count * sector_size; +} + +#elif __linux__ + +#include <linux/fs.h> + +uint64_t blkio_size(uint32_t dev) { + uint64_t block_size = 0; + if (ioctl(dev, BLKGETSIZE, &block_size) == -1) { + fprintf(stderr, "Failed to get device size\n"); + return 0; // Failed + } + + return block_size; +} + +#elif __unix__ + +#error "Platform not supported" + +#elif defined(_POSIX_VERSION) + +#error "Platform not supported" + +#else + +#error "Unknown compiler" + +#endif + + +mdev *blkio_open(const char *filename) { + uint32_t fd = open(filename, O_RDWR); + if (fd == -1) { + fprintf(stderr, "Failed to open file %s\n", filename); + fprintf(stderr, "%s (%d)\n", strerror(errno), errno); + return NULL; + } + uint64_t fsize = blkio_size(fd); + printf("File opened (fd=%u). Device size is: %llu\n", fd, fsize); + if (!fsize) { + return NULL; + } + void *base = mmap(NULL, fsize, PROT_READ, + MAP_SHARED|MAP_FILE, fd, 0); + if (base == MAP_FAILED) { + fprintf(stderr, "MMap failed.\n"); + fprintf(stderr, "%s (%d)\n", strerror(errno), errno); + return NULL; + } + if (close(fd) == -1) { + fprintf(stderr, "Failed to close file\n"); + } + mdev *ret = malloc(sizeof(mdev)); + ret->base = base; + ret->len = fsize; + ret->fd = fd; + return ret; +} + +void blkio_close(mdev *dev) { + munmap(dev->base, dev->len); + close(dev->fd); + free(dev); +} + diff --git a/osufs/osufs/blkio.h b/osufs/osufs/blkio.h new file mode 100644 index 0000000000000000000000000000000000000000..9e87fb462c951b675a9dd3bf9c4e4ab3d86c76ba --- /dev/null +++ b/osufs/osufs/blkio.h @@ -0,0 +1,31 @@ +// +// blkio.h +// osufs +// +// Created by Fang Lu on 11/28/17. +// Copyright © 2017 Fang Lu. All rights reserved. +// + +#ifndef blkio_h +#define blkio_h + +#ifdef __cplusplus +extern "C" { +#endif + +#include <inttypes.h> + +typedef struct mdev_t { + void *base; + uint64_t len; + uint32_t fd; +} mdev; + +mdev *blkio_open(const char *filename); +void blkio_close(mdev *dev); + +#ifdef __cplusplus +} +#endif + +#endif /* blkio_h */ diff --git a/osufs/osufs/main.cpp b/osufs/osufs/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c219495ff2b76a1ff05fe6c3810f47f561eec527 --- /dev/null +++ b/osufs/osufs/main.cpp @@ -0,0 +1,38 @@ +// +// main +// osufs +// +// Created by Fang Lu on 11/28/17. +// Copyright © 2017 Fang Lu. All rights reserved. +// + +#include <stdio.h> + +#include "blkio.h" + +int main(int argc, char *argv[]) { + if (argc != 2) { + fprintf(stderr, "Usage: %s device file\n", argv[0]); + return 1; + } + + mdev *dev = blkio_open(argv[1]); + if (!dev) { + fprintf(stderr, "Failed to open device file\n"); + return 1; + } + + printf("Device capacity is: %llu MiB\n", (dev->len >> 20)); + + printf("Dumping the first sector...\n"); + unsigned char *ptr = (unsigned char *)dev->base; + for (int i=0; i<512; i++) { + printf("%x ", ptr[i]); + if (i&0x7) + printf("\n"); + } + + blkio_close(dev); + + return 0; +} diff --git a/osufs/osufs/osufs.c b/osufs/osufs/osufs.c new file mode 100644 index 0000000000000000000000000000000000000000..66cc943e0676fbb1e2ca5a40251653d0d2bab7b3 --- /dev/null +++ b/osufs/osufs/osufs.c @@ -0,0 +1,9 @@ +// +// osufs.c +// osufs +// +// Created by Fang Lu on 11/28/17. +// Copyright © 2017 Fang Lu. All rights reserved. +// + +#include "osufs.h" diff --git a/osufs/osufs/osufs.h b/osufs/osufs/osufs.h new file mode 100644 index 0000000000000000000000000000000000000000..a3e445d92dfe4730a0c1af744bccca7d92806b3b --- /dev/null +++ b/osufs/osufs/osufs.h @@ -0,0 +1,23 @@ +// +// osufs.h +// osufs +// +// Created by Fang Lu on 11/28/17. +// Copyright © 2017 Fang Lu. All rights reserved. +// + +#ifndef osufs_h +#define osufs_h + +#ifdef __cplusplus +extern "C" { +#endif + +#include <stdio.h> + + +#ifdef __cplusplus +} +#endif + +#endif /* osufs_h */