Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalh committed Mar 12, 2017
0 parents commit 0bd7816
Show file tree
Hide file tree
Showing 36 changed files with 2,991 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
build
dist
doc
minix_fs_src
dokan_src
20 changes: 20 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
cmake_minimum_required (VERSION 3.1)

project (MinixFS)

if (UNIX OR MINGW)
add_compile_options(-Wall -Wno-sign-compare)
elseif (MSVC)
set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS
_CRT_SECURE_NO_WARNINGS)
add_compile_options(/W3)
endif()

file(GLOB SOURCES *.c)
file(GLOB HEADERS *.h)

add_executable(MinixFS ${SOURCES} ${HEADERS})

target_link_libraries(MinixFS ${CMAKE_SOURCE_DIR}/dokan.lib)

set_property(DIRECTORY PROPERTY VS_STARTUP_PROJECT MinixFS)
19 changes: 19 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2013 Rafa� Harabie�

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
MINIX Filesystem Driver
=======================

General
-------
This is read/write Minix Filesystem driver for Windows.
Driver operates in user mode and uses a Dokan library to communicate with the kernel.
Driver was made for educational purposes in few days and gave me a lot of fun.

Requirements
------------

Dokan has to be installed before using this driver.
You can download it from: http://dokan-dev.net/en/download/
Filesystem has been tested with version 0.6. It may not work with new versions.

Usage
-----

Listing subpartitions in disk image:

minix -f image_path -l

Mounting partition:

minix -f image_path p0s0=mount_point_path

Mount point has to be unused partition letter or path to a non-existing folder.
You can specify more than one subpartion and mount point in command line.
To unmount all partitions terminate program by pressing Ctrl+C.

Notes
-----
Using this program can make damage to your disk image, so be sure to make a backup.
Blue Screens could happen too. I am not responsible for any loss of data.
100 changes: 100 additions & 0 deletions bitmap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#include <stdlib.h>
#include <windows.h>
#include "general.h"
#include "debug.h"

static unsigned MxfsCountWordBits(uint32_t v)
{
v = v - ((v >> 1) & 0x55555555); // reuse input as temporary
v = (v & 0x33333333) + ((v >> 2) & 0x33333333); // temp
return (((v + (v >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24; // count
}

unsigned MxfsCountBitsSet(MX_BITMAP *Bm)
{
unsigned i, Result = 0;
for(i = 0; i < Bm->Size; ++i)
Result += MxfsCountWordBits(Bm->Buffer[i]);
return Result;
}

void MxfsInitBitmap(MX_BITMAP *Bm, uint32_t *Buffer, unsigned BitsCount)
{
ASSERT(MxfsCountWordBits(0) == 0);
ASSERT(MxfsCountWordBits(0x11111111) == 8);
ASSERT(MxfsCountWordBits(0xFFFFFFFF) == 32);

Bm->Buffer = Buffer;
Bm->Size = BitsCount / 32;
Bm->Hint = 0;
}

void MxfsDestroyBitmap(MX_BITMAP *Bm)
{
MxfsFree(Bm->Buffer);
Bm->Buffer = NULL;
}

int MxfsFindClearBit(MX_BITMAP *Bm)
{
unsigned i, j;

for(i = 0; i < Bm->Size; ++i)
{
if(Bm->Buffer[i] != 0xFFFFFFFF)
break;
}

if(i == Bm->Size)
{
ERR("Bitmap is full!\n");
return -ERROR_DISK_FULL;
}

for(j = 0; j < 32; ++j)
{
unsigned Bit = Bm->Buffer[i] & (1 << j);
if(!Bit) break;
}

ASSERT(j < 32);
return i * 32 + j;
}

int MxfsSetBit(MX_BITMAP *Bm, unsigned Bit)
{
unsigned i = Bit / 32;
Bit = Bit & 31;

if(i >= Bm->Size)
return -ERROR_INVALID_PARAMETER;

Bm->Buffer[i] |= (1 << Bit);
return 0;
}

int MxfsClearBit(MX_BITMAP *Bm, unsigned Bit)
{
unsigned i = Bit / 32;
Bit = Bit & 31;

if(i >= Bm->Size)
return -ERROR_INVALID_PARAMETER;

Bm->Buffer[i] &= ~(1 << Bit);
return 0;
}

int MxfsGetBit(MX_BITMAP *Bm, unsigned Bit)
{
unsigned i = Bit / 32;
Bit = Bit & 31;

if(i >= Bm->Size)
return -ERROR_INVALID_PARAMETER;

if(Bm->Buffer[i] & (1 << Bit))
return 1;
else
return 0;
}
21 changes: 21 additions & 0 deletions bitmap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef BITMAP_H_INCLUDED
#define BITMAP_H_INCLUDED

#include <stdint.h>

typedef struct
{
uint32_t *Buffer;
unsigned Size;
unsigned Hint;
} MX_BITMAP;

void MxfsInitBitmap(MX_BITMAP *Bm, uint32_t *Buffer, unsigned BitsCount);
void MxfsDestroyBitmap(MX_BITMAP *Bm);
unsigned MxfsCountBitsSet(MX_BITMAP *Bm);
int MxfsFindClearBit(MX_BITMAP *Bm);
int MxfsSetBit(MX_BITMAP *Bm, unsigned Bit);
int MxfsClearBit(MX_BITMAP *Bm, unsigned Bit);
int MxfsGetBit(MX_BITMAP *Bm, unsigned Bit);

#endif // BITMAP_H_INCLUDED
184 changes: 184 additions & 0 deletions cache.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
#include <stdlib.h>
#include "debug.h"
#include "cache.h"
#include "general.h"

void MxfsCacheInit(MINIX_FS *FileSys)
{
InitializeListHead(&FileSys->Cache.MruList);
FileSys->Cache.Count = 0;
InitializeCriticalSection(&FileSys->Cache.Lock);
}

static int MxfsCacheFlushItem(MINIX_FS *FileSys, MX_CACHE_ITEM *Item)
{
unsigned Offset = FileSys->uOffset + Item->Index*MINIX_BLOCK_SIZE;
int Result = ImgWrite(FileSys->pImg, Offset, MINIX_BLOCK_SIZE, Item->Data);
Item->Dirty = FALSE;

if(Result < 0)
ERR("MxfsCacheFlushItem: ImgWrite failed %d\n", Result);
return Result;
}

void MxfsCacheFlush(MINIX_FS *FileSys)
{
EnterCriticalSection(&FileSys->Cache.Lock);

LIST_ENTRY *Entry = FileSys->Cache.MruList.Flink;
while(Entry != &FileSys->Cache.MruList)
{
MX_CACHE_ITEM *Item = CONTAINING_RECORD(Entry, MX_CACHE_ITEM, MruList);
if(Item->Dirty)
MxfsCacheFlushItem(FileSys, Item);

Entry = Entry->Flink;
}

LeaveCriticalSection(&FileSys->Cache.Lock);
}

static void MxfsCacheDestroyLastItem(MINIX_FS *FileSys)
{
ASSERT(FileSys->Cache.Count > 0);

LIST_ENTRY *Entry = RemoveTailList(&FileSys->Cache.MruList);
MX_CACHE_ITEM *Item = CONTAINING_RECORD(Entry, MX_CACHE_ITEM, MruList);
--FileSys->Cache.Count;

if(Item->Dirty)
MxfsCacheFlushItem(FileSys, Item);

MxfsFree(Item);
}

void MxfsCacheDestroy(MINIX_FS *FileSys)
{
while(!IsListEmpty(&FileSys->Cache.MruList))
MxfsCacheDestroyLastItem(FileSys);
}

MX_CACHE_ITEM *MxfsCacheFind(MINIX_FS *FileSys, unsigned Block)
{
LIST_ENTRY *Entry = FileSys->Cache.MruList.Flink;
while(Entry != &FileSys->Cache.MruList)
{
MX_CACHE_ITEM *Item = CONTAINING_RECORD(Entry, MX_CACHE_ITEM, MruList);
if(Item->Index == Block)
return Item;

Entry = Entry->Flink;
}
return NULL;
}

MX_CACHE_ITEM *MxfsCacheLoadBlock(MINIX_FS *FileSys, unsigned Block, BOOL Zero)
{
MX_CACHE_ITEM *Item = MxfsAlloc(sizeof(MX_CACHE_ITEM));
if(!Item) return NULL;

unsigned BlockOffset = Block * MINIX_BLOCK_SIZE;
if(BlockOffset + MINIX_BLOCK_SIZE > FileSys->cbLen)
{
ERR("MxfsCacheLoadBlock: Invalid block %u\n", Block);
return NULL;
}

Item->Index = Block;

if(!Zero)
{
int Result = ImgRead(FileSys->pImg, FileSys->uOffset + BlockOffset, MINIX_BLOCK_SIZE, Item->Data);
if(Result < 0)
{
MxfsFree(Item);
ERR("MxfsCacheLoadBlock: ImgRead failed %d\n", Result);
return NULL;
}
Item->Dirty = FALSE;
} else {
memset(Item->Data, 0, MINIX_BLOCK_SIZE);
Item->Dirty = TRUE;
}

if(FileSys->Cache.Count == BLOCK_CACHE_SIZE)
MxfsCacheDestroyLastItem(FileSys);

InsertHeadList(&FileSys->Cache.MruList, &Item->MruList);
++FileSys->Cache.Count;

return Item;
}

int MxfsCacheRead(MINIX_FS *FileSys, void *Buffer, unsigned Block, unsigned Offset, unsigned Length)
{
if(!(Offset < MINIX_BLOCK_SIZE && Offset + Length <= MINIX_BLOCK_SIZE))
{
WARN("MxfsCacheRead: wrong offset (%u) or len (%u)\n", Offset, Length);
return -ERROR_INVALID_PARAMETER;
}

EnterCriticalSection(&FileSys->Cache.Lock);

int Result = -ERROR_NOT_FOUND;
MX_CACHE_ITEM *Item = MxfsCacheFind(FileSys, Block);
if(Item)
{
// Push at front
RemoveEntryList(&Item->MruList);
InsertHeadList(&FileSys->Cache.MruList, &Item->MruList);
} else
Item = MxfsCacheLoadBlock(FileSys, Block, FALSE);

if(Item)
{
memcpy(Buffer, Item->Data + Offset, Length);
Result = 0;
}

LeaveCriticalSection(&FileSys->Cache.Lock);
return Result;
}

int MxfsCacheWrite(MINIX_FS *FileSys, const void *Buffer, unsigned Block, unsigned Offset, unsigned Length)
{
if(!(Offset < MINIX_BLOCK_SIZE && Offset + Length <= MINIX_BLOCK_SIZE))
{
WARN("MxfsCacheWrite: wrong offset (%u) or len (%u)\n", Offset, Length);
return -ERROR_INVALID_PARAMETER;
}

EnterCriticalSection(&FileSys->Cache.Lock);

int Result = -ERROR_WRITE_FAULT;
MX_CACHE_ITEM *Item = MxfsCacheFind(FileSys, Block);
if(!Item)
Item = MxfsCacheLoadBlock(FileSys, Block, FALSE);

if(Item)
{
memcpy(Item->Data + Offset, Buffer, Length);
Item->Dirty = TRUE;
Result = 0;
}

LeaveCriticalSection(&FileSys->Cache.Lock);
return Result;
}

void MxfsCacheZero(MINIX_FS *FileSys, unsigned Block)
{
EnterCriticalSection(&FileSys->Cache.Lock);

MX_CACHE_ITEM *Item = MxfsCacheFind(FileSys, Block);
if(Item)
{
memset(Item->Data, 0, MINIX_BLOCK_SIZE);
Item->Dirty = TRUE;
}
else
MxfsCacheLoadBlock(FileSys, Block, TRUE);

LeaveCriticalSection(&FileSys->Cache.Lock);
}

Loading

0 comments on commit 0bd7816

Please sign in to comment.