git: c8a5e838dba4 - main - multimedia/libaacs: add MMC drive support, take maintainership

From: Vladimir Druzenko <vvd_at_FreeBSD.org>
Date: Tue, 06 Aug 2024 17:03:21 UTC
The branch main has been updated by vvd:

URL: https://cgit.FreeBSD.org/ports/commit/?id=c8a5e838dba41d61d970347baa0dc7cd38423011

commit c8a5e838dba41d61d970347baa0dc7cd38423011
Author:     Stefan Ehmann <shoesoft@gmx.net>
AuthorDate: 2024-08-06 16:58:38 +0000
Commit:     Vladimir Druzenko <vvd@FreeBSD.org>
CommitDate: 2024-08-06 16:58:38 +0000

    multimedia/libaacs: add MMC drive support, take maintainership
    
    Currently, Blu-ray playback only works if the VUK of the disc is
    available in KEYDB.cfg. Otherwise it fails.
    With this patch, libaacs will compute the key if non-revoked device keys
    and host certificate are present in KEYDB.cfg.
    The patch provides an MMC device implementation based on libcam. The
    linux implementation uses the CDROM_SEND_PACKET ioctl which is not
    available on FreeBSD.
    AACS decryption is now working in bd_info from libbluray (vlc also works).
    
    PR:     280633
---
 multimedia/libaacs/Makefile                   |   5 +-
 multimedia/libaacs/files/mmc_device_freebsd.c | 111 ++++++++++++++++++++++++++
 multimedia/libaacs/files/patch-Makefile.am    |  26 +++++-
 3 files changed, 138 insertions(+), 4 deletions(-)

diff --git a/multimedia/libaacs/Makefile b/multimedia/libaacs/Makefile
index f5179a4a0e89..aad67dbbd6b9 100644
--- a/multimedia/libaacs/Makefile
+++ b/multimedia/libaacs/Makefile
@@ -4,7 +4,7 @@ CATEGORIES=	multimedia
 MASTER_SITES=	https://get.videolan.org/${PORTNAME}/${DISTVERSION}/ \
 		https://download.videolan.org/pub/videolan/${PORTNAME}/${DISTVERSION}/
 
-MAINTAINER=	ports@FreeBSD.org
+MAINTAINER=	shoesoft@gmx.net
 COMMENT=	Advanced Access Content System implementation
 WWW=		https://www.videolan.org/developers/libaacs.html
 
@@ -23,4 +23,7 @@ CONFIGURE_ARGS=	--disable-optimizations
 
 INSTALL_TARGET=	install-strip
 
+post-extract:
+	@${CP} ${FILESDIR}/mmc_device_freebsd.c ${WRKSRC}/src/file
+
 .include <bsd.port.mk>
diff --git a/multimedia/libaacs/files/mmc_device_freebsd.c b/multimedia/libaacs/files/mmc_device_freebsd.c
new file mode 100644
index 000000000000..99b916aeddee
--- /dev/null
+++ b/multimedia/libaacs/files/mmc_device_freebsd.c
@@ -0,0 +1,111 @@
+#include <sys/types.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <cam/scsi/scsi_message.h>
+#include <camlib.h>
+
+#include "mmc_device.h"
+
+#include "util/logging.h"
+#include "util/macro.h"
+#include "util/strutl.h"
+
+#define CDB_LEN 12
+
+struct mmcdev {
+    struct cam_device *cam_dev;
+};
+
+int device_send_cmd(MMCDEV *dev, const uint8_t *cmd, uint8_t *buf, size_t tx, size_t rx)
+{
+    char str[512];
+    int result = 0;
+    union ccb *ccb;
+
+    if (buf == NULL) {
+        return 0;
+    }
+
+    ccb = cam_getccb(dev->cam_dev);
+    if (ccb == NULL) {
+        BD_DEBUG(DBG_MMC, "cam_getccb failed\n");
+        return 0;
+    }
+
+    memcpy(ccb->csio.cdb_io.cdb_bytes, cmd, CDB_LEN);
+
+    cam_fill_csio(&ccb->csio,
+            /*retries*/ 0,
+            /*cbfcnp*/ NULL,
+            /*flags*/ CAM_DEV_QFRZDIS | (tx?CAM_DIR_OUT:CAM_DIR_IN),
+            /*tag_action*/ MSG_SIMPLE_Q_TAG,
+            /*data_ptr*/ buf,
+            /*dxfer_len*/ (tx?tx:rx),
+            /*sense_len*/ SSD_FULL_SIZE,
+            /*cdb_len*/ CDB_LEN,
+            /*timeout*/ 5000);
+
+    BD_DEBUG(DBG_MMC, "Send BSD MMC cmd %s:\n", str_print_hex(str, cmd, 16));
+    if (tx) {
+        BD_DEBUG(DBG_MMC, "  Buffer: %s ->\n", str_print_hex(str, buf, tx>255?255:tx));
+    }
+
+    if (cam_send_ccb(dev->cam_dev, ccb) < 0) {
+        BD_DEBUG(DBG_MMC, "cam_send_ccb failed: %s\n", strerror(errno));
+        cam_error_print(dev->cam_dev, ccb, CAM_ESF_ALL, CAM_EPF_ALL, stderr);
+    } else if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
+        BD_DEBUG(DBG_MMC, "cam_send_ccb failed with status: %02X\n", ccb->ccb_h.status);
+        cam_error_print(dev->cam_dev, ccb, CAM_ESF_ALL, CAM_EPF_ALL, stderr);
+    } else {
+        result = 1;
+    }
+
+    cam_freeccb(ccb);
+
+    if (result > 0) {
+        if (!tx) {
+            BD_DEBUG(DBG_MMC, "  Buffer: %s <-\n", str_print_hex(str, buf, rx>255?255:rx));
+        }
+        BD_DEBUG(DBG_MMC, "  Send succeeded!\n");
+    } else {
+        BD_DEBUG(DBG_MMC, "  Send failed!\n");
+    }
+
+    return result;
+}
+
+MMCDEV *device_open(const char *path)
+{
+    struct cam_device *cam_dev;
+    cam_dev = cam_open_device(path, O_RDWR);
+    if (cam_dev == NULL) {
+        BD_DEBUG(DBG_MMC | DBG_CRIT, "cam_open_device failed: %s\n", cam_errbuf);
+        return NULL;
+    }
+
+    MMCDEV *dev = calloc(1, sizeof(MMCDEV));
+    if (!dev) {
+        cam_close_device(cam_dev);
+        return NULL;
+    }
+
+    dev->cam_dev = cam_dev;
+
+    return dev;
+}
+
+void device_close(MMCDEV **pp)
+{
+    if (pp && *pp) {
+        if ((*pp)->cam_dev != NULL) {
+            cam_close_device((*pp)->cam_dev);
+        }
+
+        X_FREE(*pp);
+    }
+}
diff --git a/multimedia/libaacs/files/patch-Makefile.am b/multimedia/libaacs/files/patch-Makefile.am
index f58b40fd59c1..9396888e2ac3 100644
--- a/multimedia/libaacs/files/patch-Makefile.am
+++ b/multimedia/libaacs/files/patch-Makefile.am
@@ -1,11 +1,31 @@
---- Makefile.am.orig	2020-11-08 18:35:24 UTC
+--- Makefile.am.orig	2024-08-05 13:50:20 UTC
 +++ Makefile.am
-@@ -7,7 +7,7 @@ SET_INCLUDES = -I$(top_srcdir)/src -I$(top_builddir)/s
+@@ -6,8 +6,8 @@ AM_CFLAGS = -std=c99 $(LIBGCRYPT_CFLAGS) $(GPG_ERROR_C
+ SET_INCLUDES = -I$(top_srcdir)/src -I$(top_builddir)/src/libaacs
  
  AM_CFLAGS = -std=c99 $(LIBGCRYPT_CFLAGS) $(GPG_ERROR_CFLAGS)
- AM_CPPFLAGS = $(SET_FEATURES) $(SET_INCLUDES)
+-AM_CPPFLAGS = $(SET_FEATURES) $(SET_INCLUDES)
 -AM_YFLAGS = -d -p libaacs_yy
++AM_CPPFLAGS = $(SET_FEATURES) $(SET_INCLUDES) -D__BSD_VISIBLE=1
 +AM_YFLAGS = -d -p libaacs_yy -Wno-yacc
  
  lib_LTLIBRARIES = libaacs.la
  libaacs_la_SOURCES=\
+@@ -63,7 +63,7 @@ libaacs_la_SOURCES+= \
+ libaacs_la_SOURCES+= \
+ 	src/file/dirs_xdg.c \
+ 	src/file/file_posix.c \
+-	src/file/mmc_device_linux.c \
++	src/file/mmc_device_freebsd.c \
+ 	src/file/path.c
+ endif
+ endif
+@@ -74,7 +74,7 @@ libaacs_la_LDFLAGS= -no-undefined -version-info $(LT_V
+ 	src/libaacs/aacs-version.h
+ 
+ libaacs_la_LDFLAGS= -no-undefined -version-info $(LT_VERSION_INFO)
+-libaacs_la_LIBADD = $(LIBGCRYPT_LIBS) $(GPG_ERROR_LIBS)
++libaacs_la_LIBADD = $(LIBGCRYPT_LIBS) $(GPG_ERROR_LIBS) -lcam
+ pkgconfigdir = $(prefix)/libdata/pkgconfig
+ pkgconfig_DATA = src/libaacs.pc
+