Skip to content

Commit

Permalink
new info
Browse files Browse the repository at this point in the history
  • Loading branch information
Print3M committed Aug 6, 2023
1 parent 147cfcb commit 46a8b4f
Show file tree
Hide file tree
Showing 39 changed files with 1,368 additions and 396 deletions.
76 changes: 76 additions & 0 deletions _blog/.hidden/reversing-soundbar-usb-protocol.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
---
slug: init-post
title: Init post
description: Init post
date: "1970-01-01"
hidden: true
---

## Init post
Init post

## How to sniff USB with usbmon and Wireshark
The `usbmon` is a kernel module which is used to collect traces of I/O operations on the USB bus. It needs to be loaded.

```bash
# Load usbmon kernel module
sudo modprobe usbmon

# Check all loaded kernel modules
lsmod
```

Now you can run Wireshark. To identify which USB device you want to sniff, you can use `lsusb` command. The **bus number** and the **device number** are essential. Every USB bus has its own number and it should be also available in the Wireshark menu as `usbmonX`. Example menu items:

```bash
enp5s0
usbmon2 <--,
usbmon1 <----- These are USB buses traffic
usbmon0 <--'
```
Now you can sniff some USB traffic. Filter out the device you want to sniff: `usb.device_address == <device-number>`.
## Get information about USB device
An example output line of `lsusb` (list all USB devices) command:
```text
Bus 001 Device 008: ID 041e:3247 Creative Technology, Ltd Sound BlasterX Katana
```
Vendor ID = `041e`, Product ID = `3247`. To get more information about that specific device use: `lsusb -v -d <vendor-id>:<product-id>`.
## Kernel modules
Show kernel modules associated with USB devices: `lsusb -vt`.
## Decoding SoundBlaster X Katana USB communication
xxx
Katana has no dedicated module and uses couple generic modules:
* [usbhid](https://github.com/torvalds/linux/tree/master/drivers/hid/usbhid)
## Problem with HID user-space drivers
Theoretically the driver uses CONTROL transfer (to the Endpoint 0) in order to change Katana's volume level. Endpoint 0 is not directly connected with any USB interface, so it should be possible not to detach actual interface driver from the kernel. Unfortunately, without detaching, an "Resource busy" error occurs. Why is that?

Katana uses CONTROL transfer indeed but with some special setup parameters:

```text
bmRequestType = 0x21,
bRequest = 1,
wValue = 0x0201 (0x0202 in the second request),
wIndex = 256,
```

The `bmRequestType` parameter is a bit-field type:

```text
0x21 in binary
0 01 00001
host-to-device (OUT) class-specific interface
```

I guess it has something to do with HID interface and because of that it doesn't work without detaching interface drivers. Unfortunately, the interfaces we have to disconnect interrupt the sound. I don't feel like implementing a whole audio driver just to change the volume. I was looking for an option to create some kind of "nested driver" that would use the default `snd-usb-audio` underneath, but would add my functionalities. I haven't found any sources on this. You'd probably have to rewrite the entire driver from scratch. The problem is that Katana uses the same interfaces to send audio and change volume level. Theoretically, it is possible to detach and reattach the driver after the script is executed, but of course `dev.attach_kernel_driver(x)` function doesn't work, so the project is stuck.

Maybe I'll figure out some way of using the default `snd-usb-audio` driver underneath in the future.
7 changes: 7 additions & 0 deletions _blog/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"editor.wordWrap": "on",
"[markdown]": {
"editor.wordWrap": "wordWrapColumn"
},
"editor.rulers": [80]
}
61 changes: 42 additions & 19 deletions _notes/bash/scripting.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,49 @@
title: Bash scripting notes
---

## Differences between Bash and sh features
- [1. Differences between Bash and sh features](#1-differences-between-bash-and-sh-features)
- [1.1. Major differences (Bash vs sh)](#11-major-differences-bash-vs-sh)
- [2. Bash references](#2-bash-references)
- [2.1. Debugging (line by line)](#21-debugging-line-by-line)
- [2.2. Comments](#22-comments)
- [2.3. Special variables](#23-special-variables)
- [2.4. Prologue](#24-prologue)
- [2.5. Defining variables](#25-defining-variables)
- [2.6. If statement](#26-if-statement)
- [2.7. Loops](#27-loops)
- [2.8. Functions](#28-functions)
- [2.9. Strings](#29-strings)
- [2.10. Math / arithmetic](#210-math--arithmetic)
- [2.11. Arrays](#211-arrays)
- [2.12. Regex](#212-regex)
- [2.13. Quick tips](#213-quick-tips)

## 1. Differences between Bash and sh features

- [GNU Majo differences from the bourne shell](https://www.gnu.org/software/bash/manual/html_node/Major-Differences-From-The-Bourne-Shell.html)

Bash is superset of sh. Sh is POSIX compliant, bash is not. Bash has many extra features which improve readability and speed of programming. Almost everything what does work on sh would be working on Bash as well, but not the other way.

### Major differences (Bash vs sh)
### 1.1. Major differences (Bash vs sh)

```bash
if [[ ... ]] vs if [ ... ]
```
## Bash references
## 2. Bash references
[Official Bash documentation (manual)](https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html)
### Debugging (line by line)
### 2.1. Debugging (line by line)
```bash
# At the beginning of the script
set -x
trap read debug
```
### Special variables
### 2.2. Comments
### 2.3. Special variables
```bash
$? # Exit code of last command
Expand All @@ -36,7 +55,7 @@ $@ # Array of arguments
$$ # Current PID
```
### Prologue
### 2.4. Prologue
```bash
#!/usr/bin/env bash # Shebang: run with Bash shell
Expand All @@ -54,7 +73,7 @@ set -e
set -o pipefail
```
### Defining variables
### 2.5. Defining variables
> **NOTE**: No spaces around equal sign!
```bash
Expand All @@ -67,7 +86,7 @@ var6=(1 5 9 43 23 43) # Array of numbers
read var7 # Read variable from stdin
```
### If statement
### 2.6. If statement
```bash
# If statements (spaces around expression matter)
Expand Down Expand Up @@ -103,7 +122,7 @@ if [[ $v1 = $v2 || $v1 != $v3 ]] # Or
if [[ $v1 = $v2 && $v1 != $v3 ]] # And
```
### Loops
### 2.7. Loops
```bash
# For loops
Expand All @@ -116,13 +135,10 @@ done
for file in /bin/* # Iterate over files
for num in {1..12..2} # {start..end..step}
for num in 1 9 4 3 3 # Interate over list of items
for item in "${arr[@]}" # Spread array to list of items (above)
for (( i=0; i<5; i++ )) # C-like for loop
for name in $(cat names.txt) # Line by line output of a command

# Iterate over array elements
for item in "${arr[@]}"; do
echo "$item"
done
for arg in "$@" # Iterate over arguments

# Iterate over lines of variable
while read -r line; do
Expand All @@ -135,7 +151,7 @@ while read -r line; do
done < file.txt
```
### Functions
### 2.8. Functions
Arguments are not named. They are only positional. Same convention as for the
script parameters.
Expand All @@ -148,7 +164,7 @@ function func1() {
func1 "test-argument" 234 # Call a function
```
### Strings
### 2.9. Strings
```bash
var='super'
Expand All @@ -170,14 +186,14 @@ str='1:2'
IFS=: read -r var1 var2 <<< "$str" => var1 == 1, var2 == 2
```
### Math / arithmetic
### 2.10. Math / arithmetic
```bash
$((1 + 1)) # Math expression
$((x + y)) # Variables math
```
### Arrays
### 2.11. Arrays
```bash
arr=("1" "2" "3") # Define array
Expand All @@ -187,7 +203,7 @@ echo ${arr[3]} # Get item
echo ${arr[@]} # Get all items
```
### Regex
### 2.12. Regex
```bash
# Check matching and group extraction
Expand All @@ -197,3 +213,10 @@ if [[ $var1 =~ $exp ]]; then
echo $BASH_REMATCH[1] # First group
fi
```
### 2.13. Quick tips
```bash
echo $RANDOM # Get random number
date +%s%N # Get UNIX timestamp (nanoseconds)
```
Loading

0 comments on commit 46a8b4f

Please sign in to comment.