<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>day49 &amp;mdash; Gerrit Niezen</title>
    <link>https://gerritniezen.com/tag:day49</link>
    <description>Maker of open-source software and hardware.</description>
    <pubDate>Wed, 15 Apr 2026 00:20:57 +0000</pubDate>
    <image>
      <url>https://i.snap.as/aMPXpIot.png</url>
      <title>day49 &amp;mdash; Gerrit Niezen</title>
      <link>https://gerritniezen.com/tag:day49</link>
    </image>
    <item>
      <title>Taking a deep dive into MTP</title>
      <link>https://gerritniezen.com/taking-a-deep-dive-into-mtp?pk_campaign=rss-feed</link>
      <description>&lt;![CDATA[I&#39;ve done a 5-part series on using MTP on MacOS with Node.js in 2018. I also did a 3-part series on using MTP on Windows with Node.js just last month. The learnings from all these posts have been incorporated into my node-mtp Node.js library.&#xA;&#xA;The node-mtp library is essentially a wrapper around the libmtp library, which means it doesn&#39;t work in the browser. Given that I eventually want this to work in the browser, and that I&#39;m struggling to get libmtp built on 32-bit Windows, I decided to write a subset of the MTP protocol myself on top of WebUSB. &#xA;&#xA;The MTP protocol spec itself can be downloaded for free from the USB Implementer&#39;s Forum. It&#39;s a 282-page document, but doesn&#39;t itself describe what the packet structure looks like. For that, you need the earlier PTP protocol that it builds on, which has been standardized as ISO 15740. Luckily the USB Implementer&#39;s Forum also has the PTP over USB spec, so you don&#39;t have to buy the ISO spec to be able to implement it. Before I discovered the PTP over USB spec, this USB device fuzzing code was pretty helpful to start decoding what&#39;s going on.&#xA;&#xA;When you connect to an MTP device over USB, you should look for an interface with three endpoints, one of which will be a bulk transfer endpoint. I captured some packets in Wireshark while running Android File Transfer on Linux, which has a nice CLI tool called aft-mtp-cli.&#xA;&#xA;The first bulk transfer packet sent to the device from the computer looks like this:&#xA;&#xA;10000000010002100000000001000000&#xA;&#xA;According to the PTP over USB spec, the structure is as follows:&#xA;&#xA;Container Length (4 bytes)&#xA;Container Type (2 bytes)&#xA;Code (2 bytes)&#xA;Transaction ID (4 bytes)&#xA;Payload&#xA;&#xA;Now we can see that10000000010002100000000001000000 can be broken down into:&#xA;&#xA;Length: 0x10000000 - 16 (in little-endian format)&#xA;Type: 0x0100 - Command Block&#xA;0x0210, or 0x1002 in big-endian - operation code OpenSession&#xA;0x00000000 - Transaction ID&#xA;0x01000000 - Payload, parameter 1: Session ID&#xA;&#xA;Next up, actually using WebUSB to start a new MTP session on the device and read some data!&#xA;&#xA;---&#xA;I’m publishing this as part of 100 Days To Offload. You can join in yourself by visiting https://100daystooffload.com.&#xA;&#xA;#100DaysToOffload #day49 #mtp&#xA; &#xA;&#xA;iComment on this post/i&#xD;&#xA;div id=&#34;cusdisthread&#34;/div]]&gt;</description>
      <content:encoded><![CDATA[<p>I&#39;ve done a <a href="https://gerritniezen.com/tag:nodemtp">5-part series</a> on using MTP on MacOS with Node.js in 2018. I also did a <a href="https://gerritniezen.com/tag:libmtp">3-part series</a> on using MTP on Windows with Node.js just last month. The learnings from all these posts have been incorporated into my <a href="https://github.com/tidepool-org/node-mtp">node-mtp</a> Node.js library.</p>

<p>The node-mtp library is essentially a wrapper around the <a href="https://github.com/libmtp/libmtp">libmtp</a> library, which means it doesn&#39;t work in the browser. Given that I eventually want this to work in the browser, and that I&#39;m struggling to get libmtp built on 32-bit Windows, I decided to write a subset of the MTP protocol myself on top of <a href="https://wicg.github.io/webusb/">WebUSB</a>.</p>

<p>The MTP protocol spec itself can be <a href="https://www.usb.org/sites/default/files/MTPv1_1.zip">downloaded for free</a> from the USB Implementer&#39;s Forum. It&#39;s a 282-page document, but doesn&#39;t itself describe what the packet structure looks like. For that, you need the earlier PTP protocol that it builds on, which has been standardized as <a href="https://www.iso.org/standard/63602.html">ISO 15740</a>. Luckily the USB Implementer&#39;s Forum also has the <a href="https://www.usb.org/document-library/still-image-capture-device-definition-10-and-errata-16-mar-2007">PTP over USB spec</a>, so you don&#39;t have to buy the ISO spec to be able to implement it. Before I discovered the PTP over USB spec, this <a href="https://github.com/ollseg/usb-device-fuzzing/blob/master/USBFuzz/MTP.py">USB device fuzzing code</a> was pretty helpful to start decoding what&#39;s going on.</p>

<p>When you connect to an MTP device over USB, you should look for an interface with three endpoints, one of which will be a bulk transfer endpoint. I captured some packets in Wireshark while running <a href="https://github.com/whoozle/android-file-transfer-linux/">Android File Transfer</a> on Linux, which has a nice CLI tool called <code>aft-mtp-cli</code>.</p>

<p>The first bulk transfer packet sent to the device from the computer looks like this:</p>

<p><code>10000000010002100000000001000000</code></p>

<p>According to the PTP over USB spec, the structure is as follows:</p>
<ul><li>Container Length (4 bytes)</li>
<li>Container Type (2 bytes)</li>
<li>Code (2 bytes)</li>
<li>Transaction ID (4 bytes)</li>
<li>Payload</li></ul>

<p>Now we can see that<code>10000000010002100000000001000000</code> can be broken down into:</p>
<ul><li>Length: <code>0x10000000</code> – 16 (in little-endian format)</li>
<li>Type: <code>0x0100</code> – Command Block</li>
<li><code>0x0210</code>, or <code>0x1002</code> in big-endian – operation code <code>OpenSession</code></li>
<li><code>0x00000000</code> – Transaction ID</li>
<li><code>0x01000000</code> – Payload, parameter 1: Session ID</li></ul>

<p>Next up, actually using WebUSB to start a new MTP session on the device and read some data!</p>

<hr/>

<p>I’m publishing this as part of 100 Days To Offload. You can join in yourself by visiting <a href="https://100daystooffload.com">https://100daystooffload.com</a>.</p>

<p><a href="https://gerritniezen.com/tag:100DaysToOffload" class="hashtag"><span>#</span><span class="p-category">100DaysToOffload</span></a> <a href="https://gerritniezen.com/tag:day49" class="hashtag"><span>#</span><span class="p-category">day49</span></a> <a href="https://gerritniezen.com/tag:mtp" class="hashtag"><span>#</span><span class="p-category">mtp</span></a></p>

<p><i>Comment on this post</i>
<div id="cusdis_thread" id="cusdis_thread"></div></p>
]]></content:encoded>
      <guid>https://gerritniezen.com/taking-a-deep-dive-into-mtp</guid>
      <pubDate>Thu, 18 Jun 2020 15:30:59 +0000</pubDate>
    </item>
  </channel>
</rss>