[
  {
    "path": ".gitignore",
    "content": ".*.swp\nfiles\nMETA-INF\nsystem.simg\nota-signed-*\nrecovery*\nplatform-tools-latest-windows.zip\nplatform-tools-latest-linux.zip\nplatform-tools-latest-darwin.zip\nplatform-tools/\n"
  },
  {
    "path": "README.md",
    "content": "NEOS\n======\n\nNEOS is the operating system for your [comma two](https://comma.ai/shop/products/comma-two-devkit) and [EON Gold Dashcam Development Kit](https://comma.ai/shop/products/eon-gold-dashcam-devkit).\n\nUpdates\n------\n\nWhen openpilot requires a NEOS update, you won't have to do anything. NEOS updates download in the background auotmatically, just like normal openpilot updates. This repo is for restoring or recovering your device.\n\nRestoring on macOS & Linux\n------\n\n1. Connect your comma two (via a USB-C to USB-A cable) or EON Gold (via a USB-mini-B to USB-A cable) to your computer\n2. Open a terminal\n3. Clone this repo `git clone https://github.com/commaai/eon-neos.git`, then `cd eon-neos`\n4. Run `./download.py`\n5. Put your device into fastboot mode by turning off your device, then holding volume down + power.\n6. Run `./flash.sh` DO NOT DISCONNECT THE DEVICE!\n\nRestoring on Windows\n------\n1. Install the Google USB driver for ADB (Android Debug Bridge)... https://developer.android.com/studio/run/win-usb\n2. Connect your comma two (via a USB-C to USB-A cable) or EON Gold (via a USB-mini-B to USB-A cable) to your computer\n3. Download and extract this repository\n4. Download & install Python 3\n5. Run download.py to download NEOS and flashing tools\n6. Put your device into fastboot mode by turning off your device, then holding volume down + power.\n7. Run flash.ps1 (right click, run with powershell). DO NOT DISCONNECT THE DEVICE!\n"
  },
  {
    "path": "download.py",
    "content": "#!/usr/bin/env python\nimport os\nimport sys\nimport json\nimport hashlib\nimport argparse\nimport zipfile\n\ntry:\n  # Python 3\n  from urllib.request import urlopen, urlretrieve\nexcept ImportError:\n  # Python 2\n  from urllib import urlopen, urlretrieve\n\nMASTER_MANIFEST = \"https://raw.githubusercontent.com/commaai/openpilot/commatwo_master/selfdrive/hardware/eon/neos.json\"\nRELEASE_MANIFEST = \"https://raw.githubusercontent.com/commaai/openpilot/release2/selfdrive/hardware/eon/neos.json\"\n\ndef download_progress(count, blockSize, totalSize):\n    if count % 1000 == 0:\n      sys.stdout.write('.')\n      sys.stdout.flush()\n\n\ndef sha256_checksum(filename, block_size=65536):\n  sha256 = hashlib.sha256()\n  with open(filename, 'rb') as f:\n    for block in iter(lambda: f.read(block_size), b''):\n      sha256.update(block)\n  return sha256.hexdigest()\n\n\ndef download(url, fhash, finalname):\n  if os.path.isfile(finalname) and sha256_checksum(finalname).lower() == fhash.lower():\n    print(\"already downloaded %s\" % url)\n    return\n\n  print(\"downloading %s with hash %s\" % (url, fhash))\n  fn = url.split(\"/\")[-1]\n  urlretrieve(url, fn, download_progress)\n  assert sha256_checksum(fn).lower() == fhash.lower()\n\n  print(\"hash check pass\")\n  os.rename(fn, finalname)\n\n\nif __name__ == \"__main__\":\n  parser = argparse.ArgumentParser(description='Download NEOS')\n  parser.add_argument('--master', action='store_true',\n                      help='Download NEOS version used on the master branch')\n\n  args = parser.parse_args()\n\n  manifest = MASTER_MANIFEST if args.master else RELEASE_MANIFEST\n\n  r = urlopen(manifest)\n\n  up = json.loads(r.read().decode())\n  download(up['recovery_url'], up['recovery_hash'], \"recovery.img\")\n  download(up['ota_url'], up['ota_hash'], \"ota-signed-latest.zip\")\n\n  with zipfile.ZipFile(\"ota-signed-latest.zip\", 'r') as z:\n    z.extractall()\n"
  },
  {
    "path": "flash.ps1",
    "content": "\r\n$fastboot = \"platform-tools/fastboot.exe\"\r\nif (Test-Path -path $fastboot) {\r\n    Write-Host 'Platform tools found';\r\n} else {\r\n    Write-Host 'Downloading platform tools';\r\n\r\n    $client = new-object System.Net.WebClient\r\n    $client.DownloadFile(\"https://dl.google.com/android/repository/platform-tools_r28.0.2-windows.zip\", \"platform-tools.zip\")\r\n\r\n    Write-Host 'Extracting platform tools';\r\n    Expand-Archive -Path \"platform-tools.zip\" -DestinationPath \".\" -Force\r\n\r\n    Remove-Item \"platform-tools.zip\"\r\n}\r\n\r\nif (Test-Path -path \"files/system.img\") {\r\n    $zip_file = Get-Item \"ota-signed-latest.zip\"\r\n    $system_img = Get-Item \"files/system.img\"\r\n\r\n    if ($zip_file.CreationTime -gt $system_img.CreationTime){\r\n        Write-Host 'Newer version found. Extracting...';\r\n        Expand-Archive -Path \"ota-signed-latest.zip\" -DestinationPath \".\" -Force\r\n    }\r\n\r\n} else {\r\n    Write-Host 'Extracting NEOS...';\r\n    Expand-Archive -Path \"ota-signed-latest.zip\" -DestinationPath \".\" -Force\r\n}\r\n\r\n\r\nInvoke-Expression \"$($fastboot) flash recovery recovery.img\"\r\nInvoke-Expression \"$($fastboot) flash boot files/boot.img\"\r\nInvoke-Expression \"$($fastboot) flash system files/system.img\"\r\n\r\nInvoke-Expression \"$($fastboot) erase userdata\"\r\nInvoke-Expression \"$($fastboot) format cache\"\r\nInvoke-Expression \"$($fastboot) reboot\"\r\n\r\n\r\nWrite-Host -NoNewLine 'Press any key to continue...';\r\n$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');"
  },
  {
    "path": "flash.sh",
    "content": "#!/bin/bash -e\n\nFASTBOOT=platform-tools/fastboot\n\nVERSION=\"r28.0.2\"\nPLATFORM=\"$(uname -s | tr '[:upper:]' '[:lower:]')\"\n\nif [ ! -f $FASTBOOT ]; then\n  rm -rf platform-tools\n  rm -f platform-tools-latest-$PLATFORM.zip\n\n  curl -L https://dl.google.com/android/repository/platform-tools_$VERSION-$PLATFORM.zip --output platform-tools.zip\n  unzip platform-tools.zip\n  rm -f platform-tools.zip\nfi\n\necho \"Please enter your computer password if prompted\"\n\nsudo $FASTBOOT oem 4F500301 || true\nsudo $FASTBOOT flash recovery recovery.img\n\n# from OTA\n[ -f files/logo.bin ] && $FASTBOOT flash LOGO files/logo.bin\nsudo $FASTBOOT flash boot files/boot.img\nsudo $FASTBOOT flash system files/system.img\n\n# clear userdata\nsudo $FASTBOOT erase userdata\nsudo $FASTBOOT format cache\nsudo $FASTBOOT reboot\n"
  }
]