[bazel] add rules for misc python scripts

There are misc. python scripts used to scramble ROM images, produce ECC
bits for flash images, and generate a version string for ROM. Bazel
rules have been added to properly refer to these scripts with Python
Bazel rules.

Signed-off-by: Timothy Trippel <ttrippel@google.com>
diff --git a/rules/autogen.bzl b/rules/autogen.bzl
index a5ba191..da543e8 100644
--- a/rules/autogen.bzl
+++ b/rules/autogen.bzl
@@ -45,14 +45,17 @@
     header = ctx.actions.declare_file("chip_info.h")
     ctx.actions.run(
         outputs = [header],
-        inputs = ctx.files.version + ctx.files._tool,
+        inputs = [
+            ctx.file.version,
+            ctx.executable._tool,
+        ],
         arguments = [
             "-o",
             header.dirname,
             "--ot_version_file",
-            ctx.files.version[0].path,
+            ctx.file.version.path,
         ],
-        executable = ctx.files._tool[0],
+        executable = ctx.executable._tool,
     )
     return [
         CcInfo(compilation_context = cc_common.create_compilation_context(
@@ -65,8 +68,15 @@
 autogen_chip_info = rule(
     implementation = _chip_info,
     attrs = {
-        "version": attr.label(default = "//util:ot_version_file", allow_files = True),
-        "_tool": attr.label(default = "//util:rom_chip_info.py", allow_files = True),
+        "version": attr.label(
+            default = "//util:ot_version_file",
+            allow_single_file = True,
+        ),
+        "_tool": attr.label(
+            default = "//util:rom_chip_info",
+            executable = True,
+            cfg = "exec",
+        ),
     },
 )
 
@@ -74,23 +84,27 @@
     output = ctx.actions.declare_file(ctx.attr.name + ".vmem")
     ctx.actions.run(
         outputs = [output],
-        inputs = ctx.files.src + ctx.files.deps + ctx.files._tool,
+        inputs = [ctx.file.src] + ctx.files.deps + [ctx.executable._tool],
         arguments = [
             "--quiet",
             "--img-cfg",
-            ctx.files.src[0].path,
+            ctx.file.src.path,
             "--out",
             output.path,
         ],
-        executable = ctx.files._tool[0],
+        executable = ctx.executable._tool,
     )
     return [DefaultInfo(files = depset([output]), data_runfiles = ctx.runfiles(files = [output]))]
 
 otp_image = rule(
     implementation = _otp_image,
     attrs = {
-        "src": attr.label(allow_files = True),
+        "src": attr.label(allow_single_file = True),
         "deps": attr.label_list(allow_files = True),
-        "_tool": attr.label(default = "//util/design:gen-otp-img.py", allow_files = True),
+        "_tool": attr.label(
+            default = "//util/design:gen-otp-img",
+            executable = True,
+            cfg = "exec",
+        ),
     },
 )
diff --git a/rules/opentitan.bzl b/rules/opentitan.bzl
index 5fd1ca8..5fce2df 100644
--- a/rules/opentitan.bzl
+++ b/rules/opentitan.bzl
@@ -275,13 +275,13 @@
         outputs = [scrambled_vmem],
         inputs = [
             ctx.file.vmem,
-            ctx.file._tool,
+            ctx.executable._tool,
         ],
         arguments = [
             ctx.file.vmem.path,
             scrambled_vmem.path,
         ],
-        executable = ctx.file._tool.path,
+        executable = ctx.executable._tool,
     )
     return [DefaultInfo(
         files = depset(outputs),
@@ -293,8 +293,9 @@
     attrs = {
         "vmem": attr.label(allow_single_file = True),
         "_tool": attr.label(
-            default = "//util/design:gen-flash-img.py",
-            allow_single_file = True,
+            default = "//util/design:gen-flash-img",
+            executable = True,
+            cfg = "exec",
         ),
     },
 )
diff --git a/util/BUILD b/util/BUILD
index db260e1..c02957a 100644
--- a/util/BUILD
+++ b/util/BUILD
@@ -16,6 +16,11 @@
 )
 
 py_binary(
+    name = "rom_chip_info",
+    srcs = ["rom_chip_info.py"],
+)
+
+py_binary(
     name = "regtool",
     srcs = ["regtool.py"],
     deps = [
diff --git a/util/design/BUILD b/util/design/BUILD
index 3b76e7c..a1d9ef8 100644
--- a/util/design/BUILD
+++ b/util/design/BUILD
@@ -2,9 +2,35 @@
 # Licensed under the Apache License, Version 2.0, see LICENSE for details.
 # SPDX-License-Identifier: Apache-2.0
 
+load("@ot_python_deps//:requirements.bzl", "requirement")
+load("@rules_python//python:defs.bzl", "py_binary")
+
 package(default_visibility = ["//visibility:public"])
 
-exports_files([
-    "gen-flash-img.py",
-    "gen-otp-img.py",
-])
+py_library(
+    name = "lib",
+    srcs = ["secded_gen.py"],
+    deps = [requirement("hjson")],
+)
+
+py_library(
+    name = "secded_gen",
+    srcs = ["secded_gen.py"],
+    deps = [requirement("hjson")],
+)
+
+py_binary(
+    name = "gen-flash-img",
+    srcs = ["gen-flash-img.py"],
+    deps = [":secded_gen"],
+)
+
+py_binary(
+    name = "gen-otp-img",
+    srcs = ["gen-otp-img.py"],
+    deps = [
+        "//util/design/lib:common",
+        "//util/design/lib:otp_mem_img",
+        requirement("hjson"),
+    ],
+)
diff --git a/util/design/lib/BUILD b/util/design/lib/BUILD
new file mode 100644
index 0000000..8df8064
--- /dev/null
+++ b/util/design/lib/BUILD
@@ -0,0 +1,49 @@
+# Copyright lowRISC contributors.
+# Licensed under the Apache License, Version 2.0, see LICENSE for details.
+# SPDX-License-Identifier: Apache-2.0
+
+load("@ot_python_deps//:requirements.bzl", "requirement")
+
+package(default_visibility = ["//visibility:public"])
+
+py_library(
+    name = "common",
+    srcs = ["common.py"],
+)
+
+py_library(
+    name = "lc_st_enc",
+    srcs = ["LcStEnc.py"],
+    deps = [
+        ":common",
+        requirement("pycryptodome"),
+    ],
+)
+
+py_library(
+    name = "otp_mem_map",
+    srcs = ["OtpMemMap.py"],
+    deps = [
+        ":common",
+        "//util/design/mubi:prim_mubi",
+        requirement("tabulate"),
+    ],
+)
+
+py_library(
+    name = "otp_mem_img",
+    srcs = ["OtpMemImg.py"],
+    deps = [
+        ":common",
+        ":lc_st_enc",
+        ":otp_mem_map",
+        ":present",
+        "//util/design/mubi:prim_mubi",
+        requirement("pycryptodome"),
+    ],
+)
+
+py_library(
+    name = "present",
+    srcs = ["Present.py"],
+)
diff --git a/util/rom_chip_info.py b/util/rom_chip_info.py
index 66b1cc0..546cb7d 100755
--- a/util/rom_chip_info.py
+++ b/util/rom_chip_info.py
@@ -7,13 +7,10 @@
 
 import argparse
 import logging as log
-import os
 import sys
 from datetime import datetime
-from io import StringIO
 from pathlib import Path
 
-
 header_template = r"""
 // Copyright lowRISC contributors.
 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
@@ -31,21 +28,20 @@
 #endif  // _F_CHIPINFO_H__
 
 """
+
+
 def main():
     parser = argparse.ArgumentParser(prog="rom_chip_info")
     parser.add_argument('--outdir',
                         '-o',
                         required=True,
-                        help='Output Directory'
-                        )
+                        help='Output Directory')
     parser.add_argument('--ot_version',
                         required=False,
-                        help='OpenTitan Version'
-                        )
+                        help='OpenTitan Version')
     parser.add_argument('--ot_version_file',
                         required=False,
-                        help='Path to a file with the OpenTitan Version'
-                        )
+                        help='Path to a file with the OpenTitan Version')
 
     log.basicConfig(format="%(levelname)s: %(message)s")
     args = parser.parse_args()
@@ -60,8 +56,7 @@
         version = open(args.ot_version_file, "rt").read().strip()
     else:
         log.error(
-            "Missing ot_version, provide --ot_version or --ot_version_file."
-        )
+            "Missing ot_version, provide --ot_version or --ot_version_file.")
         raise SystemExit(sys.exc_info()[1])
 
     outdir = Path(args.outdir)
@@ -72,8 +67,8 @@
     now = datetime.now()
     wall_time = now.strftime("%Y-%m-%d, %H:%M:%S")
 
-    log.info("Version: %s" % (version,))
-    log.info("Build Date: %s" % (wall_time,))
+    log.info("Version: %s" % (version, ))
+    log.info("Build Date: %s" % (wall_time, ))
 
     output = header_template
     output = output.replace('{%version%}', version, 1)