From 7dfe7109cad6c627cf47e0331ac203973b8e1982 Mon Sep 17 00:00:00 2001
From: shenglin <shenglin24s@163.com>
Date: Sat, 18 May 2024 13:27:32 +0800
Subject: [PATCH] Modifying code to fix compilation failure in Android
 environment

---
 Android.mk |   5 +-
 mmc.c      | 132 +++++++++++++++++++++++++++++++++++++++--------------
 mmc_cmds.c |   2 +-
 3 files changed, 103 insertions(+), 36 deletions(-)

diff --git a/Android.mk b/Android.mk
index 4b7afb1..7ca98af 100644
--- a/Android.mk
+++ b/Android.mk
@@ -1,8 +1,9 @@
 LOCAL_PATH:= $(call my-dir)
-
+GIT_VERSION := "mmc_v"
 include $(CLEAR_VARS)
 LOCAL_MODULE_TAGS := optional
-LOCAL_SRC_FILES:= mmc.c mmc_cmds.c
+LOCAL_CFLAGS += -DVERSION=\"$(GIT_VERSION)\"
+LOCAL_SRC_FILES:= mmc.c mmc_cmds.c lsmmc.c
 LOCAL_SRC_FILES += 3rdparty/hmac_sha/sha2.c 3rdparty/hmac_sha/hmac_sha2.c
 LOCAL_MODULE := mmc_utils
 LOCAL_SHARED_LIBRARIES := libcutils libc
diff --git a/mmc.c b/mmc.c
index bc8f74e..81b5bba 100644
--- a/mmc.c
+++ b/mmc.c
@@ -54,17 +54,23 @@ static struct Command commands[] = {
 	{ do_read_extcsd, -1,
 	  "extcsd read", "<device>\n"
 		"Print extcsd data from <device>.",
-	  NULL
+	  NULL,
+	  NULL,
+	  0
 	},
 	{ do_write_extcsd, 3,
 	  "extcsd write", "<offset> <value> <device>\n"
 		  "Write <value> at offset <offset> to <device>'s extcsd.",
-	  NULL
+	  NULL,
+	  NULL,
+	  0
 	},
 	{ do_writeprotect_boot_get, -1,
 	  "writeprotect boot get", "<device>\n"
 		"Print the boot partitions write protect status for <device>.",
-	  NULL
+	  NULL,
+	  NULL,
+	  0
 	},
 	{ do_writeprotect_boot_set, -1,
 	  "writeprotect boot set",
@@ -80,7 +86,9 @@ static struct Command commands[] = {
 		"  -p  Protect partition permanently instead.\n"
 		"      NOTE! -p is a one-time programmable (unreversible) change.\n"
 #endif /* DANGEROUS_COMMANDS_ENABLED */
-	  , NULL
+	  , NULL,
+	  NULL,
+	  0
 	},
 	{ do_writeprotect_user_set, -4,
 	  "writeprotect user set", "<type>" "<start block>" "<blocks>" "<device>\n"
@@ -89,42 +97,58 @@ static struct Command commands[] = {
 #else
 	  "Set the write protect configuration for the specified region\nof the user area for <device>.\n<type> must be \"none|temp|pwron\".\n    \"none\"  - Clear temporary write protection.\n    \"temp\"  - Set temporary write protection.\n    \"pwron\" - Set write protection until the next poweron.\n<start block> specifies the first block of the protected area.\n<blocks> specifies the size of the protected area in blocks.\nNOTE! The area must start and end on Write Protect Group\nboundries, Use the \"writeprotect user get\" command to get the\nWrite Protect Group size.",
 #endif /* DANGEROUS_COMMANDS_ENABLED */
-	  NULL
+	  NULL,
+	  NULL,
+	  0
 	},
 	{ do_writeprotect_user_get, -1,
 	  "writeprotect user get", "<device>\n"
 		"Print the user areas write protect configuration for <device>.",
-	  NULL
+	  NULL,
+	  NULL,
+	  0
 	},
 	{ do_disable_512B_emulation, -1,
 	  "disable 512B emulation", "<device>\n"
 		"Set the eMMC data sector size to 4KB by disabling emulation on\n<device>.",
-	  NULL
+	  NULL,
+	  NULL,
+	  0
 	},
 	{ do_create_gp_partition, -6,
 	  "gp create", "<-y|-n|-c> " "<length KiB> " "<partition> " "<enh_attr> " "<ext_attr> " "<device>\n"
 		"Create general purpose partition for the <device>.\nDry-run only unless -y or -c is passed.\nUse -c if more partitioning settings are still to come.\nNOTE!  This is a one-time programmable (unreversible) change.\nTo set enhanced attribute to general partition being created set\n <enh_attr> to 1 else set it to 0.\nTo set extended attribute to general partition\n set <ext_attr> to 1,2 else set it to 0",
-	  NULL
+	  NULL,
+	  NULL,
+	  0
 	},
 	{ do_enh_area_set, -4,
 	  "enh_area set", "<-y|-n|-c> " "<start KiB> " "<length KiB> " "<device>\n"
 		"Enable the enhanced user area for the <device>.\nDry-run only unless -y or -c is passed.\nUse -c if more partitioning settings are still to come.\nNOTE!  This is a one-time programmable (unreversible) change.",
-	  NULL
+	  NULL,
+	  NULL,
+	  0
 	},
 	{ do_write_reliability_set, -2,
 	  "write_reliability set", "<-y|-n|-c> " "<partition> " "<device>\n"
 		"Enable write reliability per partition for the <device>.\nDry-run only unless -y or -c is passed.\nUse -c if more partitioning settings are still to come.\nNOTE!  This is a one-time programmable (unreversible) change.",
-	  NULL
+	  NULL,
+	  NULL,
+	  0
 	},
 	{ do_status_get, -1,
 	  "status get", "<device>\n"
 	  "Print the response to STATUS_SEND (CMD13).",
-	  NULL
+	  NULL,
+	  NULL,
+	  0
 	},
 	{ do_write_boot_en, -3,
 	  "bootpart enable", "<boot_partition> " "<send_ack> " "<device>\n"
 		"Enable the boot partition for the <device>.\nDisable the boot partition for the <device> if <boot_partition> is set to 0.\nTo receive acknowledgment of boot from the card set <send_ack>\nto 1, else set it to 0.",
-	  NULL
+	  NULL,
+	  NULL,
+	  0
 	},
 	{ do_boot_bus_conditions_set, -4,
 	  "bootbus set", "<boot_mode> " "<reset_boot_bus_conditions> " "<boot_bus_width> " "<device>\n"
@@ -132,7 +156,9 @@ static struct Command commands[] = {
 	  "<boot_mode> must be \"single_backward|single_hs|dual\"\n"
 	  "<reset_boot_bus_conditions> must be \"x1|retain\"\n"
 	  "<boot_bus_width> must be \"x1|x4|x8\"",
-	  NULL
+	  NULL,
+	  NULL,
+	  0
 	},
 	{ do_write_bkops_en, -2,
 	  "bkops_en", "<auto|manual> <device>\n"
@@ -140,22 +166,30 @@ static struct Command commands[] = {
 		"The auto (AUTO_EN) setting is only supported on eMMC 5.0 or newer.\n"
 		"Setting auto won't have any effect if manual is set.\n"
 		"NOTE!  Setting manual (MANUAL_EN) is one-time programmable (unreversible) change.",
-	  NULL
+	  NULL,
+	  NULL,
+	  0
 	},
 	{ do_hwreset_en, -1,
 	  "hwreset enable", "<device>\n"
 		"Permanently enable the eMMC H/W Reset feature on <device>.\nNOTE!  This is a one-time programmable (unreversible) change.",
-	  NULL
+	  NULL,
+	  NULL,
+	  0
 	},
 	{ do_hwreset_dis, -1,
 	  "hwreset disable", "<device>\n"
 		"Permanently disable the eMMC H/W Reset feature on <device>.\nNOTE!  This is a one-time programmable (unreversible) change.",
-	  NULL
+	  NULL,
+	  NULL,
+	  0
 	},
 	{ do_sanitize, -1,
 	  "sanitize", "<device> [timeout_ms]\n"
 		"Send Sanitize command to the <device>.\nThis will delete the unmapped memory region of the device.",
-	  NULL
+	  NULL,
+	  NULL,
+	  0
 	},
 	{ do_rpmb_write_key, -1,
 	  "rpmb write-key", "<rpmb device> <key file>\n"
@@ -166,12 +200,16 @@ static struct Command commands[] = {
 		  "Example:\n"
 		  "  $ echo -n AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHH | \\\n"
 		  "    mmc rpmb write-key /dev/mmcblk0rpmb -",
-	  NULL
+	  NULL,
+	  NULL,
+	  0
 	},
 	{ do_rpmb_read_counter, -1,
 	  "rpmb read-counter", "<rpmb device>\n"
 		  "Counter value for the <rpmb device> will be read to stdout.",
-	  NULL
+	  NULL,
+	  NULL,
+	  0
 	},
 	{ do_rpmb_read_block, -1,
 	  "rpmb read-block", "<rpmb device> <address> <blocks count> <output file> [key file]\n"
@@ -184,7 +222,9 @@ static struct Command commands[] = {
 		  "    mmc rpmb read-block /dev/mmcblk0rpmb 0x02 2 /tmp/block -\n"
 		  "or read two blocks without verification\n"
 		  "  $ mmc rpmb read-block /dev/mmcblk0rpmb 0x02 2 /tmp/block",
-	  NULL
+	  NULL,
+	  NULL,
+	  0
 	},
 	{ do_rpmb_write_block, -1,
 	  "rpmb write-block", "<rpmb device> <address> <256 byte data file> <key file>\n"
@@ -195,44 +235,60 @@ static struct Command commands[] = {
 		  "  $ (awk 'BEGIN {while (c++<256) printf \"a\"}' | \\\n"
 		  "    echo -n AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHH) | \\\n"
 		  "    mmc rpmb write-block /dev/mmcblk0rpmb 0x02 - -",
-	  NULL
+	  NULL,
+	  NULL,
+	  0
 	},
 	{ do_cache_en, -1,
 	  "cache enable", "<device>\n"
 		"Enable the eMMC cache feature on <device>.\n"
 		"NOTE! The cache is an optional feature on devices >= eMMC4.5.",
-	  NULL
+	  NULL,
+	  NULL,
+	  0
 	},
 	{ do_cache_dis, -1,
 	  "cache disable", "<device>\n"
 		"Disable the eMMC cache feature on <device>.\n"
 		"NOTE! The cache is an optional feature on devices >= eMMC4.5.",
-	  NULL
+	  NULL,
+	  NULL,
+	  0
 	},
 	{ do_read_csd, -1,
 	  "csd read", "<device path>\n"
 		  "Print CSD data from <device path>.\n"
 		  "The device path should specify the csd file directory.",
-	  NULL
+	  NULL,
+	  NULL,
+	  0
 	},
+	#if 1
 	{ do_read_cid, -1,
 	  "cid read", "<device path>\n"
 		  "Print CID data from <device path>.\n"
 		  "The device path should specify the cid file directory.",
-	  NULL
+	  NULL,
+	  NULL,
+	  0
 	},
 	{ do_read_scr, -1,
 	  "scr read", "<device path>\n"
 		  "Print SCR data from <device path>.\n"
 		  "The device path should specify the scr file directory.",
-	  NULL
+	  NULL,
+	  NULL,
+	  0
 	},
+	#endif
 	{ do_ffu, -2,
 	  "ffu", "<image name> <device> [chunk-bytes]\n"
 		"Run Field Firmware Update with <image name> on <device>.\n"
 		"[chunk-bytes] is optional and defaults to its max - 512k. "
 		"should be in decimal bytes and sector aligned.\n",
-	  NULL
+	  NULL,
+	  NULL,
+	  0
 	},
 	{ do_erase, -4,
 	"erase", "<type> " "<start address> " "<end address> " "<device>\n"
@@ -240,7 +296,9 @@ static struct Command commands[] = {
 		"NOTE!: This will delete all user data in the specified region of the device\n"
 		"<type> must be: legacy | discard | secure-erase | "
 		"secure-trim1 | secure-trim2 | trim \n",
-	NULL
+	NULL,
+	  NULL,
+	  0
 	},
 	{ do_general_cmd_read, -1,
 	"gen_cmd read", "<device> [arg]\n"
@@ -248,17 +306,23 @@ static struct Command commands[] = {
 		"NOTE!: [arg] is optional and defaults to 0x1. If [arg] is specified, then [arg]\n"
 		"must be a 32-bit hexadecimal number, prefixed with 0x/0X. And bit0 in [arg] must\n"
 		"be 1.",
-	NULL
+	NULL,
+	  NULL,
+	  0
 	},
 	{ do_softreset, -1,
 	  "softreset", "<device>\n"
 	  "Issues a CMD0 softreset, e.g. for testing if hardware reset for UHS works",
-	  NULL
+	  NULL,
+	  NULL,
+	  0
 	},
 	{ do_preidle, -1,
 	  "preidle", "<device>\n"
 	  "Issues a CMD0 GO_PRE_IDLE",
-	  NULL
+	  NULL,
+	  NULL,
+	  0
 	},
 	{ do_alt_boot_op, -1,
 	  "boot_operation", "<boot_data_file> <device>\n"
@@ -270,9 +334,11 @@ static struct Command commands[] = {
 	  "2. The MMC must currently be running at the bus mode that is configured for the boot operation (HS200 and HS400 not supported at all).\n"
 	  "3. Only up to 512K bytes of boot data will be transferred.\n"
 	  "4. The MMC will perform a soft reset, if your system cannot handle that do not use the boot operation from mmc-utils.\n",
-	  NULL
+	  NULL,
+	  NULL,
+	  0
 	},
-	{ 0, 0, 0, 0 }
+	{ 0, 0, 0, 0, NULL, NULL, 0}
 };
 
 static char *get_prgname(char *programname)
diff --git a/mmc_cmds.c b/mmc_cmds.c
index ae7b876..4fdcc1a 100644
--- a/mmc_cmds.c
+++ b/mmc_cmds.c
@@ -29,7 +29,7 @@
 #include <stdint.h>
 #include <assert.h>
 #include <linux/fs.h> /* for BLKGETSIZE */
-
+#include <endian.h>
 #include "mmc.h"
 #include "mmc_cmds.h"
 #include "3rdparty/hmac_sha/hmac_sha2.h"
-- 
2.34.1

