From 5d339ca82211b7bdecd12ddcd14cf9dcc1dbb98d Mon Sep 17 00:00:00 2001
From: Petr Hodina <phodina@protonmail.com>
Date: Mon, 21 Feb 2022 13:33:41 +0100
Subject: [PATCH v2] gnu: Add rkdeveloptool-pine64.

* gnu/packages/hardware.scm (rkdeveloptool-pine64): New variable.
* gnu/local.mk: Add patches.
* gnu/packages/patches/rkdeveloptool-pine64-speed-up-xfer.patch: New file.
* gnu/packages/patches/rkdeveloptool-pine64-dump-over-2gib.patch: New file.

diff --git a/gnu/local.mk b/gnu/local.mk
index f0b1b3fd15..177ad1996b 100644
--- a/gnu/local.mk
+++ b/gnu/local.mk
@@ -1800,6 +1800,8 @@ dist_patch_DATA =						\
   %D%/packages/patches/restic-0.9.6-fix-tests-for-go1.15.patch	\
   %D%/packages/patches/retroarch-LIBRETRO_DIRECTORY.patch	\
   %D%/packages/patches/r-mixedpower-r2power.patch		\
+  %D%/packages/patches/rkdeveloptool-pine64-speed-up-xfer.patch \
+  %D%/packages/patches/rkdeveloptool-pine64-dump-over-2gib.patch \
   %D%/packages/patches/rnp-add-version.cmake.patch		\
   %D%/packages/patches/rnp-disable-ruby-rnp-tests.patch		\
   %D%/packages/patches/rnp-unbundle-googletest.patch		\
diff --git a/gnu/packages/hardware.scm b/gnu/packages/hardware.scm
index 81f38d069b..e86d96b792 100644
--- a/gnu/packages/hardware.scm
+++ b/gnu/packages/hardware.scm
@@ -15,6 +15,7 @@
 ;;; Copyright © 2022 Marcel Kupiec <formbi@protonmail.com>
 ;;; Copyright © 2022 Maxim Cournoyer <maxim.cournoyer@gmail.com>
 ;;; Copyright © 2022 Efraim Flashner <efraim@flashner.co.il>
+;;; Copyright © 2023 Petr Hodina <phodina@protonmail.com>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -1174,6 +1175,43 @@ (define-public rkdeveloptool
 as the Pinebook Pro.")
       (license license:gpl2+))))
 
+(define-public rkdeveloptool-pine64
+  (package
+    (name "rkdeveloptool-pine64")
+    (version "1.1.0")
+    (source (origin
+              (method git-fetch)
+              (uri (git-reference
+                    (url
+                     "https://gitlab.com/pine64-org/quartz-bsp/rkdeveloptool")
+                    (commit version)))
+              (file-name (git-file-name name version))
+              (patches (search-patches
+                        "rkdeveloptool-pine64-speed-up-xfer.patch"
+                        "rkdeveloptool-pine64-dump-over-2gib.patch"))
+              (sha256
+               (base32
+                "0nh9592mllygycnxbw91vg58wwais7w3w62rl9gcvc4m3i909b1z"))))
+    (build-system meson-build-system)
+    (arguments
+     `(#:tests? #f ;no test suite
+       #:phases (modify-phases %standard-phases
+                  ;; attempts to place the file into the udev pkg read-only path
+                  (add-after 'unpack 'fix-udev-path
+                    (lambda* _
+                      (substitute* "meson.build"
+                        (("udev_rules_dir,")
+                         (string-append "'" %output "/lib/udev/rules.d',"))))))))
+    (native-inputs (list pkg-config))
+    (inputs (list eudev libusb))
+    (synopsis "Read from and write to RockChip devices over USB")
+    (description
+     "Rkdeveloptool is a fastboot-like CLI tool to read from and
+write to RockChip devices over USB.  Supports PineNote and Quartz64 as well
+as other Pine64 RK devices.")
+    (home-page "https://gitlab.com/pine64-org/quartz-bsp/rkdeveloptool")
+    (license license:gpl2+)))
+
 (define-public usbguard
   (package
     (name "usbguard")
diff --git a/gnu/packages/patches/rkdeveloptool-pine64-dump-over-2gib.patch b/gnu/packages/patches/rkdeveloptool-pine64-dump-over-2gib.patch
new file mode 100644
index 0000000000..e2c7328bfa
--- /dev/null
+++ b/gnu/packages/patches/rkdeveloptool-pine64-dump-over-2gib.patch
@@ -0,0 +1,54 @@
+diff --git a/main.cpp b/main.cpp
+index 029f0e2..e40bce2 100644
+--- a/main.cpp
++++ b/main.cpp
+@@ -2455,12 +2455,6 @@ bool read_lba(
+ 
+ 	if (!check_device_type(dev, RKUSB_LOADER | RKUSB_MASKROM))
+ 		return false;
+-	if (uiLen % 512) {
+-		fprintf(stderr, "Length must be a multiple of sector size (%d)\n",
+-				SECTOR_SIZE);
+-		return false;
+-	}
+-	uiLen /= 512;
+ 
+ 	if (!bRet) {
+ 		fprintf(stderr, "Read LBA quit, creating comm object failed!\n");
+@@ -3265,7 +3259,7 @@ bool handle_command(int argc, char *argv[], CRKScan *pScan) {
+ 				bRet = get_lba_from_gpt(master_gpt, argv[2], &lba, &lba_end);
+ 				if (bRet) {
+ 					bSuccess = read_lba(dev, (u32)lba,
+-							((u32)(lba_end - lba + 1)) * 512, argv[3]);
++							(u32)(lba_end - lba + 1), argv[3]);
+ 				} else {
+ 					fprintf(stderr, "Could not find the %s partition\n", argv[2]);
+ 				}
+@@ -3276,7 +3270,7 @@ bool handle_command(int argc, char *argv[], CRKScan *pScan) {
+ 	} else if (strcmp(strCmd.c_str(), "RL") == 0 ||
+ 			strcmp(strCmd.c_str(), "READ") == 0) {
+ 		char *pszEnd;
+-		UINT uiBegin, uiLen;
++		UINT uiBegin;
+ 		if (argc != 5)
+ 			fprintf(stderr, "Usage: rkdeveloptool read start-sector num-bytes "
+ 				   "filename\n");
+@@ -3285,11 +3279,15 @@ bool handle_command(int argc, char *argv[], CRKScan *pScan) {
+ 			if (*pszEnd)
+ 				fprintf(stderr, "Begin is invalid, please check!\n");
+ 			else {
+-				uiLen = strtoul(argv[3], &pszEnd, 0);
++				unsigned long long len = strtoull(argv[3], &pszEnd, 0);
+ 				if (*pszEnd)
+ 					fprintf(stderr, "Len is invalid, please check!\n");
+-				else {
+-					bSuccess = read_lba(dev, uiBegin, uiLen, argv[4]);
++				else if (len % 512) {
++					fprintf(stderr, "Length must be a multiple of sector size (%d)\n",
++                		SECTOR_SIZE);
++				} else {
++					len /= 512;
++					bSuccess = read_lba(dev, uiBegin, len, argv[4]);
+ 				}
+ 			}
+ 		}
-- 
2.38.1

