Skip to content

Commit 8f91cb9

Browse files
committed
Switched to C++ for some code.
Added support for discard/TRIM. Now tells the kernel to re-read the partition table.
1 parent 2e7342c commit 8f91cb9

13 files changed

Lines changed: 353 additions & 157 deletions

File tree

Makefile

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
.SUFFIXES:
2+
3+
# Sources and defines
4+
TARGET := $(notdir $(CURDIR))
5+
BUILD := build
6+
INCLUDES := include
7+
SOURCES := source
8+
DEFINES :=
9+
10+
11+
# Compiler settings
12+
ARCH :=
13+
CFLAGS := $(ARCH) -std=c17 -O2 -g -fstrict-aliasing -ffunction-sections \
14+
-fdata-sections -Wall -Wextra -Wstrict-aliasing=2
15+
CXXFLAGS := $(ARCH) -std=c++20 -O2 -g -fstrict-aliasing -ffunction-sections \
16+
-fdata-sections -Wall -Wextra -Wstrict-aliasing=2
17+
ASFLAGS := $(ARCH) -O2 -g -x assembler-with-cpp
18+
ARFLAGS := -rcs
19+
LDFLAGS := $(ARCH) -O2 -g -Wl,--gc-sections
20+
21+
PREFIX :=
22+
CC := $(PREFIX)gcc
23+
CXX := $(PREFIX)g++
24+
AS := $(PREFIX)gcc
25+
AR := $(PREFIX)gcc-ar
26+
27+
28+
# Do not change anything after this
29+
ifneq ($(BUILD),$(notdir $(CURDIR)))
30+
31+
export OUTPUT := $(CURDIR)/$(TARGET)
32+
export VPATH := $(foreach dir,$(DATA),$(CURDIR)/$(dir)) \
33+
$(foreach dir,$(SOURCES),$(CURDIR)/$(dir))
34+
35+
CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
36+
CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
37+
SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s)))
38+
39+
ifeq ($(strip $(CPPFILES)),)
40+
export LD := $(CC)
41+
else
42+
export LD := $(CXX)
43+
endif
44+
45+
export OFILES := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o)
46+
47+
export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) -I$(CURDIR)/$(BUILD)
48+
49+
50+
.PHONY: $(BUILD) clean release
51+
52+
$(BUILD):
53+
@[ -d $@ ] || mkdir -p $@
54+
@$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile
55+
56+
clean:
57+
@echo clean ...
58+
@rm -rf $(BUILD) $(TARGET)
59+
60+
release:
61+
@[ -d $(BUILD) ] || mkdir -p $(BUILD)
62+
@$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile NO_DEBUG=1
63+
64+
else
65+
66+
ifneq ($(strip $(NO_DEBUG)),)
67+
DEFINES += -DNDEBUG
68+
endif
69+
70+
#VERS_STRING := $(shell git describe --tags --match v[0-9]* --abbrev=8 | sed 's/-[0-9]*-g/-/i')
71+
#VERS_MAJOR := $(shell echo "$(VERS_STRING)" | sed 's/v\([0-9]*\)\..*/\1/i')
72+
#VERS_MINOR := $(shell echo "$(VERS_STRING)" | sed 's/.*\.\([0-9]*\).*/\1/')
73+
74+
#DEFINES += -DVERS_STRING=\"$(VERS_STRING)\"
75+
#DEFINES += -DVERS_MAJOR=$(shell echo "$(VERS_STRING)" | sed 's/v\([0-9]*\)\..*/\1/i')
76+
#DEFINES += -DVERS_MINOR=$(shell echo "$(VERS_STRING)" | sed 's/.*\.\([0-9]*\).*/\1/')
77+
78+
79+
# Main target
80+
$(OUTPUT): $(OFILES)
81+
$(LD) $(LDFLAGS) $(OFILES) $(LIBPATHS) $(LIBS) -o $@
82+
@echo built ... $(notdir $@)
83+
84+
85+
%.o: %.cpp
86+
@echo $(notdir $<)
87+
$(CXX) $(CXXFLAGS) $(DEFINES) $(INCLUDE) -c $< -o $@
88+
89+
%.o: %.c
90+
@echo $(notdir $<)
91+
$(CC) $(CFLAGS) $(DEFINES) $(INCLUDE) -c $< -o $@
92+
93+
%.o: %.s
94+
@echo $(notdir $<)
95+
$(AS) $(ASFLAGS) $(DEFINES) $(INCLUDE) -c $< -o $@
96+
97+
%.a:
98+
@echo $(notdir $@)
99+
$(AR) $(ARFLAGS) $@ $^
100+
101+
endif

blockdev.h

Lines changed: 0 additions & 12 deletions
This file was deleted.

compile.sh

Lines changed: 0 additions & 3 deletions
This file was deleted.

format.h

Lines changed: 0 additions & 9 deletions
This file was deleted.

include/blockdev.h

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#pragma once
2+
3+
#include "types.h"
4+
5+
6+
7+
class BlockDev
8+
{
9+
bool m_isRegularFile;
10+
bool m_isDevDirty;
11+
int m_fd;
12+
u64 m_diskSectors; // TODO: Store sector size along with this. ioctl(..., BLKSSZGET, ...). Or maybe BLKPBSZGET like dosfstools?
13+
14+
15+
BlockDev(const BlockDev&) noexcept = delete; // Copy
16+
BlockDev(BlockDev&&) noexcept = delete; // Move
17+
18+
BlockDev& operator =(const BlockDev&) noexcept = delete; // Copy
19+
BlockDev& operator =(BlockDev&&) noexcept = delete; // Move
20+
21+
22+
public:
23+
BlockDev(void) noexcept : m_isRegularFile(false), m_isDevDirty(false), m_fd(-1), m_diskSectors(0) {}
24+
~BlockDev(void) noexcept
25+
{
26+
if(m_fd != -1) close();
27+
}
28+
29+
/**
30+
* @brief Opens the block device.
31+
*
32+
* @param[in] path The path.
33+
* @param[in] rw When true open device in read + write mode.
34+
*
35+
* @return Returns 0 on success or errno.
36+
*/
37+
int open(const char *const path, const bool rw = false) noexcept;
38+
39+
/**
40+
* @brief Returns the number of sectors.
41+
*
42+
* @return The number of sectors.
43+
*/
44+
u64 getSectors(void) const noexcept {return m_diskSectors;}
45+
46+
/**
47+
* @brief Reads sectors from the block device.
48+
*
49+
* @param buf The output buffer.
50+
* @param[in] sector The start sector.
51+
* @param[in] count The number of sectors to read.
52+
*
53+
* @return Returns 0 on success or errno.
54+
*/
55+
int read(u8 *buf, const u64 sector, const u64 count) const noexcept;
56+
57+
/**
58+
* @brief Writes sectors to the block device.
59+
*
60+
* @param[in] buf The input buffer.
61+
* @param[in] sector The start sector.
62+
* @param[in] count The number of sectors to write.
63+
*
64+
* @return Returns 0 on success or errno.
65+
*/
66+
int write(const u8 *buf, const u64 sector, const u64 count) noexcept;
67+
68+
/**
69+
* @brief Truncates to the specified size if the device is a regular file. Otherwise not supported.
70+
*
71+
* @param[in] sectors The size in sectors.
72+
*
73+
* @return Returns 0 on success or errno.
74+
*/
75+
int truncate(const u64 sectors) noexcept;
76+
77+
/**
78+
* @brief Perform a TRIM/erase on the whole block device. Not supported for regular files.
79+
*
80+
* @param[in] secure If true do a secure erase.
81+
*
82+
* @return Returns 0 on success or errno.
83+
*/
84+
int discardAll(const bool secure = false) const noexcept;
85+
86+
/**
87+
* @brief Closes the block device.
88+
*/
89+
void close(void) noexcept;
90+
};

include/format.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
3+
#include <stdbool.h>
4+
#include "types.h"
5+
6+
7+
#define FLAGS_DRY_RUN (1u)
8+
#define FLAGS_ERASE (1u<<1)
9+
#define FLAGS_SECURE_ERASE (1u<<2)
10+
#define FLAGS_FORCE_FAT32 (1u<<3)
11+
#define FLAGS_PRINT_FS (1u<<4)
12+
#define FLAGS_VERBOSE (1u<<5)
13+
14+
15+
16+
void setVerboseMode(const bool verbose);
17+
u32 formatSd(const char *const path, const char *const label, const u32 flags, const u64 overrTotSec);
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)