Skip to content

Upstream the script thats used to generate patch for AST2700#568

Open
chander-nexthop wants to merge 5 commits into
sonic-net:masterfrom
nexthop-ai:chander-ast2700-patch-script
Open

Upstream the script thats used to generate patch for AST2700#568
chander-nexthop wants to merge 5 commits into
sonic-net:masterfrom
nexthop-ai:chander-ast2700-patch-script

Conversation

@chander-nexthop
Copy link
Copy Markdown
Contributor

@chander-nexthop chander-nexthop commented May 7, 2026

As discussed in the SONiC-BMC work group meeting yesterday I am submitting the script that I use to generate the patch file to add support for AST 2700 to the SONiC Linux Kernel. The script by default will create the patch based on the current top-of-tree in aspeed-master-v6.12. This can be overridded by running the script with the env var ASPEED_TAG, ex "ASPEED_TAG=v00.07.02 ./scripts/generate-aspeed-patch.sh"

… 2700

2. Script to generate the patch that adds support for Aspeed AST2700
   SOC. The script will generate the patch in WORK_DIR/aspeed-ast2700-support-new.patch
   This needs to be moved to patches-sonic/aspeed-ast2700-support.patch.

Signed-off-by: Chandrasekaran Swaminathan <chander@nexthop.ai>
@chander-nexthop chander-nexthop requested a review from a team as a code owner May 7, 2026 08:30
@mssonicbld
Copy link
Copy Markdown

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

@paulmenzel
Copy link
Copy Markdown
Contributor

Thank you for sharing the script. Please make this two merge/pull requests.

Signed-off-by: Chandrasekaran Swaminathan <chander@nexthop.ai>
@mssonicbld
Copy link
Copy Markdown

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

@chander-nexthop
Copy link
Copy Markdown
Contributor Author

Thank you for sharing the script. Please make this two merge/pull requests.

Done.

@chander-nexthop chander-nexthop changed the title Fix an unwanted option and upstream the script thats used to generate patch for AST2700 Upstream the script thats used to generate patch for AST2700 May 7, 2026
Comment thread scripts/generate-aspeed-patch.sh Outdated
else
echo "Warning: Patch file not found: $line"
fi
done < <(head -228 "$KERNEL_DIR/patches-sonic/series")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the first 228 lines specifically?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @saiarcot895 for pointing it out . It a hardcoded cutoff for the pre aspeed section of the series file . it is replaced in the latest commit with an aspeed section skip so it adapts as patches are added or reordered.

Comment thread scripts/generate-aspeed-patch.sh Outdated
Comment on lines +176 to +242
cat > "$WORK_DIR/smart_merge.py" << 'PYTHON_EOF'
#!/usr/bin/env python3
"""
Smart merge for Kconfig/Makefile files.
Uses a common ancestor approach: extract Aspeed additions from diff,
add them to SONiC base at correct locations while preserving order.
"""
import sys
import re
import difflib

def smart_merge(sonic_file, aspeed_file, target_file):
# Aspeed-related patterns
aspeed_patterns = [
re.compile(r'aspeed', re.IGNORECASE),
re.compile(r'ast2[567]00', re.IGNORECASE),
re.compile(r'ast1[78]00', re.IGNORECASE),
re.compile(r'AST2700_IRQ'),
re.compile(r'ARCH_ASPEED'),
]

def is_aspeed_related(text):
return any(pattern.search(text) for pattern in aspeed_patterns)

# Read files
with open(sonic_file, 'r') as f:
sonic_lines = f.readlines()

with open(aspeed_file, 'r') as f:
aspeed_lines = f.readlines()

# Use difflib to get a proper sequence matcher
matcher = difflib.SequenceMatcher(None, sonic_lines, aspeed_lines)

result = []

for tag, i1, i2, j1, j2 in matcher.get_opcodes():
if tag == 'equal':
# Common lines - keep them
result.extend(sonic_lines[i1:i2])
elif tag == 'delete':
# Lines deleted in aspeed - KEEP THEM (preserve SONiC content)
result.extend(sonic_lines[i1:i2])
elif tag == 'insert':
# Lines added in aspeed - check if Aspeed-related
new_lines = aspeed_lines[j1:j2]
aspeed_related = any(is_aspeed_related(line) for line in new_lines)
if aspeed_related:
# Add these lines
result.extend(new_lines)
# Otherwise skip (non-Aspeed additions)
elif tag == 'replace':
# Lines changed - keep SONiC version, then add Aspeed additions if any
result.extend(sonic_lines[i1:i2])
new_lines = aspeed_lines[j1:j2]
aspeed_related = any(is_aspeed_related(line) for line in new_lines)
if aspeed_related:
# Also add the Aspeed replacements
result.extend(new_lines)

# Write result
with open(target_file, 'w') as f:
f.writelines(result)

if __name__ == '__main__':
smart_merge(sys.argv[1], sys.argv[2], sys.argv[3])
PYTHON_EOF
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion for the future: split this into a separate file just for editing/review purposes.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is taken care @saiarcot895 . extracted into scripts/smart_merge.py now. Thank you.

@mssonicbld
Copy link
Copy Markdown

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

Copy link
Copy Markdown
Contributor

@paulmenzel paulmenzel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

700 lines is quite much. Why can’t nexthop not publish a git repository with the all the SONiC patches applied, and the Aspeed patches on top? (SONiC should also publish it’s own Linux kernel git archive with the patches applied.)

Comment thread scripts/generate-aspeed-patch.sh Outdated
# Example, run with ASPEED_TAG=v00.07.02 ./scripts/generate-aspeed-patch.sh
ASPEED_BRANCH="${ASPEED_TAG:-aspeed-master-v6.12}"
ASPEED_REPO="https://github.com/AspeedTech-BMC/linux.git"
WORK_DIR="/tmp/aspeed-patch-gen"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please consider $TMPDIR.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @paulmenzel for the feedback. review comment incorporated .

Comment thread scripts/generate-aspeed-patch.sh Outdated
OUTPUT_PATCH="$WORK_DIR/aspeed-ast2700-support-new.patch"

# Read kernel version from Makefile
KERNEL_VERSION=$(grep "^KERNEL_VERSION" "$KERNEL_DIR/Makefile" | cut -d= -f2 | tr -d ' ')
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sed -n 's/^KERNEL_VERSION\s*=\s*//p' "$KERNEL_DIR/Makefile"

Copy link
Copy Markdown

@chinmoy-nexthop chinmoy-nexthop May 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

KERNEL_VERSION ?= 6.12.41

A literal = regex doesnt match ?=, so the variable would expand to empty and break the rest of the script. I generalized the pattern. Thank you for feedback .

Comment thread scripts/generate-aspeed-patch.sh Outdated
fi
mkdir -p "$SONIC_SRC"
cd "$SONIC_SRC"
wget -q "$SONIC_KERNEL_URL"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why redownload it each time?

Copy link
Copy Markdown

@chinmoy-nexthop chinmoy-nexthop May 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tarball is now cached . Thank you.

Comment thread scripts/generate-aspeed-patch.sh Outdated
wget -q "$SONIC_KERNEL_URL"
tar -xf "linux_${KERNEL_VERSION}.orig.tar.xz" --strip-components=1
rm "linux_${KERNEL_VERSION}.orig.tar.xz"
echo "SONiC kernel source ready: $(du -sh $SONIC_SRC | cut -f1)"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explicitly mention the size?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explicitly mentioned the size now. Thank you.

- Updated patch series file to move ASPEED patches at the end

- Move inline smart_merge Python out of the bash heredoc into
  scripts/smart_merge.py; bash invokes it via $SMART_MERGE.

- Replace the hard-coded "head -228" cap with a break at the
  "###-> aspeed" marker so the patch-application loop stays correct
  as patches-sonic/series evolves.

- Warn patches listed after "###-> aspeed-end"
  with a note that they may need regeneration if they touch files
  also modified by the regenerated aspeed patch.

Signed-off-by: Chinmoy Dey <chinmoy@nexthop.ai>
@chinmoy-nexthop chinmoy-nexthop force-pushed the chander-ast2700-patch-script branch from df0c469 to 5e82787 Compare May 19, 2026 09:16
@mssonicbld
Copy link
Copy Markdown

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines will not run the associated pipelines, because the pull request was updated after the run command was issued. Review the pull request again and issue a new run command.

- WORK_DIR now honors ${TMPDIR:-/tmp}.
- KERNEL_VERSION read via sed (handles ?= / := / += / =).
- SONiC kernel tarball is cached across runs; only the extracted
  $SONIC_SRC tree is wiped and re-extracted each time.
- progress lines now label the "size on disk"
  figure explicitly.

Merge remote-tracking branch 'upstream/master' into chander-ast2700-patch-script

Signed-off-by: Chinmoy Dey <chinmoy@nexthop.ai>
@mssonicbld
Copy link
Copy Markdown

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants