Spdk/scripts/spdk-gpt.py
paul luse 17538bdc67 add (c) and SPDX header to python files as needed
per Intel policy to include file commit date using git cmd
below.  The policy does not apply to non-Intel (C) notices.

git log --follow -C90% --format=%ad --date default <file> | tail -1

and then pull just the year from the result.

Intel copyrights were not added to files where Intel either had
no contribution ot the contribution lacked substance (ie license
header updates, formatting changes, etc)

Note that several files in this patch didn't end the license/(c)
block with a blank comment line so these were added as the vast
majority of files do have this last blank line.  Simply there for
consistency.

Signed-off-by: paul luse <paul.e.luse@intel.com>
Change-Id: I6cd3f18d1b469d5ef249d26ddb2923ca6b970bd4
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/15208
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: Ben Walker <benjamin.walker@intel.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
2022-11-10 08:28:53 +00:00

75 lines
2.2 KiB
Python
Executable File

#!/usr/bin/env python3
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (C) 2021 Intel Corporation
# All rights reserved.
#
import argparse
import os
import stat
import struct
def block_exists(block):
return os.path.exists(block) and stat.S_ISBLK(os.stat(block).st_mode)
def lbsize(block):
if not os.path.exists("/sys/block/%s/queue/logical_block_size" % block):
return 512
with open("/sys/block/%s/queue/logical_block_size" % block) as lbs:
return int(lbs.read())
def readb(block, offset, length, format="Q"):
b = os.open(block, os.O_RDONLY)
os.lseek(b, offset, os.SEEK_SET)
data = os.read(b, length)
os.close(b)
return struct.unpack(format, data)[0]
def is_spdk_gpt(block, entry):
block_path = "/dev/" + block
if not block_exists(block_path):
print("%s is not a block device" % block)
return False
disk_lbsize = lbsize(block)
gpt_sig = 0x5452415020494645 # EFI PART
spdk_guid = [0x7c5222bd, 0x8f5d, 0x4087, 0x9c00, 0xbf9843c7b58c]
if readb(block_path, disk_lbsize, 8) != gpt_sig:
print("No valid GPT data, bailing")
return False
part_entry_lba = disk_lbsize * readb(block_path, disk_lbsize + 72, 8)
part_entry_lba = part_entry_lba + (entry - 1) * 128
guid = [
readb(block_path, part_entry_lba, 4, "I"),
readb(block_path, part_entry_lba + 4, 2, "H"),
readb(block_path, part_entry_lba + 6, 2, "H"),
readb(block_path, part_entry_lba + 8, 2, ">H"),
readb(block_path, part_entry_lba + 10, 8, ">Q") >> 16
]
return guid == spdk_guid
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Checks if SPDK GUID is present on given block device')
parser.add_argument('block', type=str, help='block device to check')
parser.add_argument('-e', '--entry', dest='entry', help='GPT partition entry',
required=False, type=int, default=1)
args = parser.parse_args()
try:
if is_spdk_gpt(args.block.replace("/dev/", ""), args.entry):
exit(0)
exit(1)
except Exception as e:
print("Failed to read GPT data from %s (%s)" % (args.block, e))
exit(1)