Skip to content

Commit

Permalink
FlashIdDecoder: implement Kioxia and WD
Browse files Browse the repository at this point in the history
  • Loading branch information
PeratX committed Jun 23, 2021
1 parent e07cb11 commit 81bb843
Show file tree
Hide file tree
Showing 9 changed files with 407 additions and 2 deletions.
4 changes: 4 additions & 0 deletions FlashDetector/src/iTXTech/FlashDetector/Arrayable.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,8 @@ private static function transformArray(array $a) : array{
}
return $a;
}

public function __toString(){
return json_encode($this->toArray(), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ class WesternDigital extends Decoder{
"064G" => 512 * Constants::DENSITY_GBITS,
"128G" => Constants::DENSITY_TBITS,
"256G" => 2 * Constants::DENSITY_TBITS,
"512G" => 4 * Constants::DENSITY_TBITS
"512G" => 4 * Constants::DENSITY_TBITS,
"1T00" => 8 * Constants::DENSITY_TBITS
];

public static function getName() : string{
Expand Down
29 changes: 29 additions & 0 deletions FlashDetector/src/iTXTech/FlashDetector/FlashDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
use iTXTech\FlashDetector\Decoder\WesternDigitalShortCode;
use iTXTech\FlashDetector\Decoder\Yangtze;
use iTXTech\FlashDetector\Fdb\Fdb;
use iTXTech\FlashDetector\FlashId\Decoder as FlashIdDecoder;
use iTXTech\FlashDetector\FlashId\Kioxia as KioxiaIdDecoder;
use iTXTech\FlashDetector\FlashId\WesternDigital as WesternDigitalIdDecoder;
use iTXTech\FlashDetector\Processor\Processor;
use iTXTech\FlashDetector\Property\Classification;
use iTXTech\SimpleFramework\Util\StringUtil;
Expand All @@ -45,6 +48,10 @@ abstract class FlashDetector{

/** @var Decoder[] */
private static $decoders = [];

/** @var FlashIdDecoder[] */
private static $flashIdDecoders = [];

/** @var Fdb */
private static $fdb;
private static $mdb = [];
Expand Down Expand Up @@ -126,6 +133,9 @@ public static function initialize(){
self::registerDecoder(WesternDigital::class);
self::registerDecoder(WesternDigitalShortCode::class);
self::registerDecoder(Yangtze::class);

self::registerFlashIdDecoder(new KioxiaIdDecoder());
self::registerFlashIdDecoder(new WesternDigitalIdDecoder());
}

public static function registerDecoder(string $decoder) : bool{
Expand All @@ -137,6 +147,25 @@ public static function registerDecoder(string $decoder) : bool{
return false;
}

public static function registerFlashIdDecoder(KioxiaIdDecoder $decoder) : bool{
if(is_a($decoder, FlashIdDecoder::class, true)){
/** @var $decoder FlashIdDecoder */
self::$flashIdDecoders[] = $decoder;
return true;
}
return false;
}

public static function decodeFlashId(string $id) : ?FlashIdInfo {
$id = hexdec($id);
foreach(self::$flashIdDecoders as $decoder){
if($decoder->check($id)){
return $decoder->decode($id);
}
}
return null;
}

public static function detect(string $partNumber, bool $combineFdb = true) : FlashInfo{
$partNumber = str_replace([" ", ",", "&", ".", "|"], "", strtoupper($partNumber));
foreach(self::$decoders as $decoder){
Expand Down
64 changes: 64 additions & 0 deletions FlashDetector/src/iTXTech/FlashDetector/FlashId/Decoder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

/*
* iTXTech FlashDetector
*
* Copyright (C) 2018-2021 iTX Technologies
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/

namespace iTXTech\FlashDetector\FlashId;

use iTXTech\FlashDetector\FlashIdInfo;

abstract class Decoder{
public $def;
public $vendorName;
public $vendorId;

public function __construct(string $vendorName, int $vendorId, array $def){
$this->vendorName = $vendorName;
$this->vendorId = $vendorId;
$this->def = $def;
}

public function check(int $id) : bool{
if(($id > 0x100000000000 and $id < 0x1000000000000) and $id >> 40 == $this->vendorId){
return true;
}
return false;
}

public function decode(string $id) : FlashIdInfo{
return self::decodeIdDef($id, $this->def, (new FlashIdInfo($id))->setVendor($this->vendorName));
}

public function decodeIdDef(int $id, array $def, FlashIdInfo $info) : FlashIdInfo{
foreach($def as $offset => $rules){
$byte = ($id >> (8 * (6 - $offset))) & 0xff;
foreach($rules as $name => $rule){
$data = 0;
foreach($rule["dq"] as $dq){
$data = ($data << 1) + (($byte >> $dq) & 0b1);
}
if(isset($rule["def"][$data])){
$info->{"set" . ucfirst($name)}($rule["def"][$data]);
}
}
}
return $info;
}
}
140 changes: 140 additions & 0 deletions FlashDetector/src/iTXTech/FlashDetector/FlashId/Kioxia.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php

/*
* iTXTech FlashDetector
*
* Copyright (C) 2018-2021 iTX Technologies
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/

namespace iTXTech\FlashDetector\FlashId;

use iTXTech\FlashDetector\Constants;
use iTXTech\FlashDetector\FlashIdInfo;

class Kioxia extends Decoder{
public const ID_DEFINITION = [
2 => [
"density" => [
"dq" => [7, 6, 5, 4, 3, 2, 1, 0],
"def" => [
0b11010011 => 8 * Constants::DENSITY_GBITS, //D3
0b11010101 => 16 * Constants::DENSITY_GBITS, //D5
0b11010111 => 32 * Constants::DENSITY_GBITS, //D7
0b11011110 => 64 * Constants::DENSITY_GBITS, //DE
0b00111010 => 128 * Constants::DENSITY_GBITS, //3A
0b00111100 => 256 * Constants::DENSITY_GBITS, //3C
0b00111110 => 512 * Constants::DENSITY_GBITS, //3E
0b01001000 => 1 * Constants::DENSITY_TBITS, //48
0x49 => 2 * Constants::DENSITY_TBITS,
0x40 => 4 * Constants::DENSITY_TBITS,
0x4C => 256 * Constants::DENSITY_GBITS
]
]
],
3 => [
"die" => [
"dq" => [1, 0],
"def" => [
0b00 => 1,
0b01 => 2,
0b10 => 4,
0b11 => 8
]
],
"cellLevel" => [
"dq" => [3, 2],
"def" => [
0b00 => 1, //SLC
0b01 => 2, //MLC
0b10 => 3, //TLC
0b11 => 4, //QLC
]
]
],
4 => [
"pageSize" => [
"dq" => [1, 0],
"def" => [
0b00 => 2, //2KB
0b01 => 4, //4KB
0b10 => 8, //8KB
0b11 => 16, //16KB
]
],
"blockSize" => [
"dq" => [7, 5, 4],
"def" => [
0b000 => 128,
0b001 => 256,
0b010 => 512,
0b011 => 1024,
0b100 => 2048,
0b101 => 4096,
0b110 => 6144,
0b111 => 0
]
]
],
5 => [
"plane" => [
"dq" => [3, 2],
"def" => [
0b00 => 1,
0b01 => 2,
0b10 => 4,
0b11 => 8
]
]
],
6 => [
"toggle" => [
"dq" => [7],
"def" => [
0b0 => false,
0b1 => true
]
],
"processNode" => [
"dq" => [6, 5, 2, 1, 0],
"def" => [
0b10100 => "43nm",
0b10101 => "32nm",
0b10110 => "24nm",
0b10111 => "19nm",
0b10000 => "A19nm",
0b10001 => "15nm",
0b11001 => "BiCS2",
0b11010 => "BiCS3",
0b11011 => "BiCS4"
]

]
]
];

public function __construct(){
parent::__construct(Constants::VENDOR_KIOXIA, 0x98, self::ID_DEFINITION);
}

public function decode(string $id) : FlashIdInfo{
$info = parent::decode($id);
if($info->getDie() > 0 and $info->getPlane() > 0){
return $info->setPlane($info->getPlane() / $info->getDie());
}
return $info;
}
}
31 changes: 31 additions & 0 deletions FlashDetector/src/iTXTech/FlashDetector/FlashId/WesternDigital.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* iTXTech FlashDetector
*
* Copyright (C) 2018-2021 iTX Technologies
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/

namespace iTXTech\FlashDetector\FlashId;

use iTXTech\FlashDetector\Constants;

class WesternDigital extends Kioxia{
public function __construct(){
Decoder::__construct(Constants::VENDOR_WESTERN_DIGITAL, 0x45, self::ID_DEFINITION);
}
}
Loading

0 comments on commit 81bb843

Please sign in to comment.