Closed agaviola80 closed 7 months ago
@agaviola80 Thank you for your contribution! I'll try to merge it soon, but I'm quite busy at the moment...
You're welcome and thanks as well @Ognian! It's okay, no rush from my end.
Hi @Ognian,
Please use this second patch (sdmon.c.patch2) instead of the previous as I fixed the problem on the use of open() O_RDWR flag which causes an issue "error1":"1st CMD56 CALL FAILED: Bad file descriptor" in obtaining data in Kingston industrial microSD card and it's the reason why I use O_RDONLY flag. This time its the same with Linux with O_RDWR flag. Aside from that I got other fixes on the display formatting with long double types. See attached zip'ed file below for this second patch.
root@kingston:/tmp/sdmon-latest/src # cat sdmon.c.patch2
--- /tmp/sdmon-latest/src/sdmon.c.orig 2024-03-27 17:52:49.237357000 +0800
+++ /tmp/sdmon-latest/src/sdmon.c 2024-03-27 18:16:04.075170000 +0800
@@ -17,7 +17,9 @@
// this include defines MMC_BLOCK_MAJOR the magic number for the calculation of
// MMC_IOC_CMD
+#if defined(__linux__)
#include <linux/major.h>
+#endif
// since this include/linux/mmc/core.h is not available we pasted it in here
/* From kernel linux/mmc/core.h */
@@ -47,7 +49,11 @@
#include <errno.h>
// this is the include defining the mmc_ioc_cmd struct and mmc_ioc_cmd_set_data
// helper etc ...
+#if defined(__linux__)
#include <linux/mmc/ioctl.h>
+#elif defined(__FreeBSD__)
+#include <dev/mmc/mmc_ioctl.h>
+#endif
#include <time.h>
@@ -62,7 +68,11 @@
struct mmc_ioc_cmd idata;
memset(&idata, 0, sizeof(idata));
+#if defined(__linux__)
memset(lba_block_data, 0, sizeof(__u8) * SD_BLOCK_SIZE);
+#elif defined(__FreeBSD__)
+ memset(lba_block_data, 0, sizeof(SD_BLOCK_SIZE));
+#endif
idata.write_flag = 0;
idata.opcode = SD_GEN_CMD;
@@ -83,7 +93,11 @@
char data_out[SD_BLOCK_SIZE];
memset(&idata, 0, sizeof(idata));
+#if defined(__linux__)
memset(&data_out[0], 0, sizeof(__u8) * SD_BLOCK_SIZE);
+#elif defined(__FreeBSD__)
+ memset(&data_out[0], 0, sizeof(SD_BLOCK_SIZE));
+#endif
idata.write_flag = 1;
idata.opcode = SD_GEN_CMD;
@@ -191,6 +205,12 @@
}
printf("\"addTime\": \"%s\",\n", formatBool(addTime));
+ /*
+ * With FreeBSD, make sure kern.geom.debugflags sysctl
+ * flag has value of 16. To change, invoke the command
+ * "sysctl kern.geom.debugflags=16" or write it in the
+ * /etc/sysctl.conf.
+ */
fd = open(device, O_RDWR);
if (fd < 0) {
printf("\"error\":\"device open failed\"\n}\n");
@@ -302,12 +322,19 @@
printf("\"Total spare blocks cnt\": %d,\n", data_in[24]);
printf("\"Factory bad blocks cnt\": %d,\n", data_in[25]);
printf("\"Runtime bad blocks cnt\": %d,\n", data_in[26]);
- printf("\"Spare utilization rate\": %d%,\n", data_in[27]);
+ printf("\"Spare utilization rate\": %d%%,\n", data_in[27]);
printf("\"SPOR failure cnt\": %d,\n", bytes_to_int(data_in[28], data_in[29], data_in[30], data_in[31]));
+#if defined(__linux__)
printf("\"Minimum erase cnt\": %d,\n", (long)((data_in[35]) + (data_in[34]) + (data_in[33]) + data_in[32]));
printf("\"Maximum erase cnt\": %d,\n", (long)((data_in[39]) + (data_in[38]) + (data_in[37]) + data_in[36]));
printf("\"Total erase cnt\": %d,\n", (long)((data_in[43]) + (data_in[42]) + (data_in[41]) + data_in[40]));
printf("\"Average erase cnt\": %d,\n", (long)((data_in[47]) + (data_in[46]) + (data_in[45]) + data_in[44]));
+#elif defined(__FreeBSD__)
+ printf("\"Minimum erase cnt\": %ld,\n", (long)((data_in[35]) + (data_in[34]) + (data_in[33]) + data_in[32]));
+ printf("\"Maximum erase cnt\": %ld,\n", (long)((data_in[39]) + (data_in[38]) + (data_in[37]) + data_in[36]));
+ printf("\"Total erase cnt\": %ld,\n", (long)((data_in[43]) + (data_in[42]) + (data_in[41]) + data_in[40]));
+ printf("\"Average erase cnt\": %ld,\n", (long)((data_in[47]) + (data_in[46]) + (data_in[45]) + data_in[44]));
+#endif
printf("\"FW version\": %c%c%c%c%c%c%c,\n", data_in[53], data_in[54], data_in[55], data_in[56], data_in[57], data_in[58], data_in[59]);
close(fd);
printf("\"success\":true\n}\n");
Here are the outputs with and without time.
root@kingston:/tmp/sdmon-latest/src # ./sdmon /dev/mmcsd0
{
"version": "",
"date": "2024-03-27T18:45:29.000Z",
"device":"/dev/mmcsd0",
"addTime": "false",
"read_via_cmd56_arg_1":"read successful but signature 0xff 0xff",
"idata.response[]":"0x900 0x00 0x00 0x00",
"flashId": ["0x98","0x3c","0x98","0xb3","0xf6","0xe3","0x08","0x1e","0x00"],
"icVersion": ["0x1f","0xc3"],
"fwVersion": [38,240],
"ceNumber": "0x03",
"spareBlockCount": 14,
"initialBadBlockCount": 34,
"goodBlockRatePercent": 99.41,
"totalEraseCount": 1375,
"enduranceRemainLifePercent": 100.00,
"avgEraseCount": 1,
"minEraseCount": 1,
"maxEraseCount": 11,
"powerUpCount": 56,
"abnormalPowerOffCount": 0,
"totalRefreshCount": 0,
"productMarker": ["0x70","0x53","0x4c","0x43","0x00","0x00","0x00","0x00"],
"laterBadBlockCount": 0,
"success":true
}
root@kingston:/tmp/sdmon-latest/src # ./sdmon /dev/mmcsd0 -a
{
"version": "",
"date": "2024-03-27T19:01:51.000Z",
"device":"/dev/mmcsd0",
"addTime": "true",
"read_via_cmd56_arg_1":"read successful but signature 0xff 0xff",
"idata.response[]":"0x900 0x00 0x00 0x00",
"flashId": ["0x98","0x3c","0x98","0xb3","0xf6","0xe3","0x08","0x1e","0x00"],
"icVersion": ["0x1f","0xc3"],
"fwVersion": [38,240],
"ceNumber": "0x03",
"spareBlockCount": 14,
"initialBadBlockCount": 34,
"goodBlockRatePercent": 99.41,
"totalEraseCount": 1375,
"enduranceRemainLifePercent": 100.00,
"avgEraseCount": 1,
"minEraseCount": 1,
"maxEraseCount": 11,
"powerUpCount": 56,
"abnormalPowerOffCount": 0,
"totalRefreshCount": 0,
"productMarker": ["0x70","0x53","0x4c","0x43","0x00","0x00","0x00","0x00"],
"laterBadBlockCount": 0,
"success":true
}
Thanks, Archimedes
not sure if I added everything, since "latest" was ambiguous ...
Hi @Ognian,
Thanks for merging the patch! I just tried the latest release v0.9.0
and it's working with my FreeBSD in Raspberry Pi 3B with Kingston SDCIT2/32GB Industrial microSD card.
Check the source (after downlod and unpacking).
root@kingston:~/sdmon/sdmon-0.9.0/src # ls -lah
total 3992
drwxrwxr-x 2 root wheel 512B Apr 24 12:11 .
drwxrwxr-x 5 root wheel 512B Apr 23 00:29 ..
-rw-rw-r-- 1 root wheel 325B Apr 23 00:29 Makefile
-rw-rw-r-- 1 root wheel 324B Apr 23 00:29 Makefile.freebsd
-rwxr-xr-x 1 root wheel 3.8M Apr 24 12:11 sdmon
-rw-rw-r-- 1 root wheel 26K Apr 23 00:29 sdmon.c
Compile source.
root@kingston:~/sdmon/sdmon-0.9.0/src # make -f Makefile.freebsd
/usr/bin/cc -Wall -g -D_REENTRANT -DVERSION="\"\"" -static -c sdmon.c -o sdmon.o
/usr/bin/cc -Wall -g -D_REENTRANT -DVERSION="\"\"" -static -o sdmon sdmon.o
Check the compiled binary (sdmon).
root@kingston:~/sdmon/sdmon-0.9.0/src # ls -lah
total 3992
drwxrwxr-x 2 root wheel 512B Apr 24 12:11 .
drwxrwxr-x 5 root wheel 512B Apr 23 00:29 ..
-rw-rw-r-- 1 root wheel 325B Apr 23 00:29 Makefile
-rw-rw-r-- 1 root wheel 324B Apr 23 00:29 Makefile.freebsd
-rwxr-xr-x 1 root wheel 3.8M Apr 24 12:11 sdmon
-rw-rw-r-- 1 root wheel 26K Apr 23 00:29 sdmon.c
-rw-r--r-- 1 root wheel 41K Apr 24 12:11 sdmon.o
Enable disk write in FreeBSD.
root@kingston:~/sdmon/sdmon-0.9.0/src # sysctl kern.geom.debugflags=16
kern.geom.debugflags: 0 -> 16
Tests and results.
root@kingston:~/sdmon/sdmon-0.9.0/src # ./sdmon /dev/mmcsd0
{
"version": "",
"date": "2024-04-24T12:13:01.000",
"device":"/dev/mmcsd0",
"addTime": "false",
"read_via_cmd56_arg_1":"read successful but signature 0xff 0xff",
"idata.response[]":"0x900 0x00 0x00 0x00",
"flashId": ["0x98","0x3c","0x98","0xb3","0xf6","0xe3","0x08","0x1e","0x00"],
"icVersion": ["0x1f","0xc3"],
"fwVersion": [38,240],
"ceNumber": "0x03",
"spareBlockCount": 14,
"initialBadBlockCount": 34,
"goodBlockRatePercent": 99.41,
"totalEraseCount": 1500,
"enduranceRemainLifePercent": 100.00,
"avgEraseCount": 1,
"minEraseCount": 1,
"maxEraseCount": 13,
"powerUpCount": 78,
"abnormalPowerOffCount": 0,
"totalRefreshCount": 0,
"productMarker": ["0x70","0x53","0x4c","0x43","0x00","0x00","0x00","0x00"],
"laterBadBlockCount": 0,
"success":true
}
root@kingston:~/sdmon/sdmon-0.9.0/src # ./sdmon /dev/mmcsd0 -a { "version": "", "date": "2024-04-24T12:33:51.000", "device":"/dev/mmcsd0", "addTime": "true", "read_via_cmd56_arg_1":"read successful but signature 0xff 0xff", "idata.response[]":"0x900 0x00 0x00 0x00", "flashId": ["0x98","0x3c","0x98","0xb3","0xf6","0xe3","0x08","0x1e","0x00"], "icVersion": ["0x1f","0xc3"], "fwVersion": [38,240], "ceNumber": "0x03", "spareBlockCount": 14, "initialBadBlockCount": 34, "goodBlockRatePercent": 99.41, "totalEraseCount": 1500, "enduranceRemainLifePercent": 100.00, "avgEraseCount": 1, "minEraseCount": 1, "maxEraseCount": 13, "powerUpCount": 78, "abnormalPowerOffCount": 0, "totalRefreshCount": 0, "productMarker": ["0x70","0x53","0x4c","0x43","0x00","0x00","0x00","0x00"], "laterBadBlockCount": 0, "success":true }
Verifying with Linux.
1. Check the source (after downlod and unpacking).
root@agaviola:~/sdmon/sdmon-0.9.0/src# ls -lah total 532K drwxrwxr-x 2 root root 4.0K Apr 24 12:09 . drwxrwxr-x 5 root root 4.0K Apr 23 00:29 .. -rw-rw-r-- 1 root root 325 Apr 23 00:29 Makefile -rw-rw-r-- 1 root root 324 Apr 23 00:29 Makefile.freebsd -rw-rw-r-- 1 root root 26K Apr 23 00:29 sdmon.c
2. Compile source.
root@agaviola:~/sdmon/sdmon-0.9.0/src# make /usr/bin/gcc -Wall -g -D_REENTRANT -DVERSION="\"\"" -static -c sdmon.c /usr/bin/gcc -Wall -g -D_REENTRANT -DVERSION="\"\"" -static -o sdmon sdmon.o
4. Check the compiled binary (sdmon).
root@agaviola:~/sdmon/sdmon-0.9.0/src# ls -lah total 532K drwxrwxr-x 2 root root 4.0K Apr 24 12:09 . drwxrwxr-x 5 root root 4.0K Apr 23 00:29 .. -rw-rw-r-- 1 root root 325 Apr 23 00:29 Makefile -rw-rw-r-- 1 root root 324 Apr 23 00:29 Makefile.freebsd -rwxr-xr-x 1 root root 460K Apr 24 12:09 sdmon -rw-rw-r-- 1 root root 26K Apr 23 00:29 sdmon.c -rw-r--r-- 1 root root 27K Apr 24 12:09 sdmon.o
5. Test (sdmon is working but the result have error CMD56 CALL FAILED because this type of SanDisk microSD (Extreme PRO 32GB) is a commercial grade not Industrial, no response to CMD56 request)
root@agaviola:~/sdmon/sdmon-0.9.0/src# ./sdmon /dev/mmcblk0 { "version": "", "date": "2024-04-24T12:09:39.000", "device":"/dev/mmcblk0", "addTime": "false", "read_via_cmd56_arg_1":"not implemented: Connection timed out", "idata.response[]":"0x900 0x00 0x00 0x00", "error2":"2nd CMD56 CALL FAILED: Connection timed out" }
Thanks,
Archimedes
@agaviola80 thanks a lot for testing! If I find time I'll try to add building the FreeBSD into github actions. A starter link would be this but I don'T know if this is somehow supported under github actions...
You're welcome @Ognian!
Haven't tried that but I found this one https://github.com/vmactions, not sure if this is what you're looking. As far as I know there were some people working on porting the Docker in FreeBSD before but it wasn't pursued for some reason. Virtualization such as Qemu https://wiki.freebsd.org/QemuRecipes and Jails https://wiki.freebsd.org/Jails are the most commonly used.
Hi,
I would like to submit two patches, the
sdmon.c.patch
andMakefile.freebsd.patch
to make it work with FreeBSD particularly 14.0-RELEASE. My patches are made using thesdmon-latest
source using my Raspberry Pi 3B SBC and it has been tested with my SanDisk Industrial microSD card and Kingston SDCIT2 Industrial microSD card.root@sandiski:~ # cat /home/freebsd/sdmon-latest/src/sdmon.c.patch
I created a new Makefile.freebsd with contents to be able to compile FreeBSD in its native Clang compiler.
root@sandiski:/home/freebsd/sdmon-latest/src # cat Makefile.freebsd
and then compile
root@sandiski:/home/freebsd/sdmon-latest/src # make -f Makefile.freebsd
Here are the test results:
SanDisk Industrial (with and without time)
root@sandiski:/home/freebsd/sdmon-latest/src # ./sdmon /dev/mmcsd0
root@sandiski:/home/freebsd/sdmon-latest/src # ./sdmon /dev/mmcsd0 -a
Kingston SCIT2 (with and without time)
root@kingston:/tmp/sdmon-latest/src # ./sdmon /dev/mmcsd0
root@kingston:/tmp/sdmon-latest/src # ./sdmon /dev/mmcsd0 -a
I also perform regression testing with my Ubuntu Linux 16.04 were it is able to compile and run the program with my changes though my microSD is SanDisk Ultra, so it does not support health monitoring.
root@bpi-iot-ros-ai:/home/pi/sdmon-latest/src# make
root@bpi-iot-ros-ai:/home/pi/sdmon-latest/src# ./sdmon /dev/mmcblk0
Lastly, attaching the patches for your reference.
sdmon.c.patch Makefile.freebsd.patch
Thanks, Archimedes